What is the code for getting a refreshed Facebook token in an Android app?

前端 未结 1 1360
悲哀的现实
悲哀的现实 2021-02-09 03:47

I am developing an Android app. With the impending deprecation of Facebook\'s offline_access permission I am trying to use the Graph API to extend the Facebook token.



        
1条回答
  •  忘了有多久
    2021-02-09 04:07

    To be honest, I'm a bit confused - looks like you have everything to get it done - and it's simple. But let me try to answer your question. Here is the code from my C# project where I extend app's token with my comments in case you are not familiar with C# languages and classes:

    string currentToken = "token from somewhere";
    
    // WebClient is used to send GET request to the given URL
    // and receive the response
    using (var req = new System.Net.WebClient())
    {
        // create URL string and fill it in with data (app Id, secret, and current token)
        var extendTokenUrl = string.Format(
            "https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type=fb_exchange_token&fb_exchange_token={2}",
            FB_APP_ID,
            FB_APP_SECRET,
            currentToken);
    
        // send GET request and download the response
        var response = req.DownloadString(extendTokenUrl);
    
        // if all is good, response will be a string which looks like this:
        // access_token=<>
        var newToken = response.Substring("access_token=".Length);
    
        // now save newToken in your DB INSTEAD of currentToken - 
        // so all calls will be made with extended token
        SaveTokenInDB(newToken);
    }
    

    hope that helps, and translating this into Java should be straightforward.

    0 讨论(0)
提交回复
热议问题