How to renew Facebook access token using its C# SDK

烂漫一生 提交于 2019-12-07 15:27:31

问题


I'm using the Facebook C# SDK to fetch as much data, e.g. posts, comments, user info, from Facebook as possible, but my program stops after my access token expires after certain perior of time, and I have to restart the program. I got the access token from the Facebook developer tools, but how can I renew the token? It is TOTO in http://csharpsdk.org/docs/web/handling-expired-access-tokens.


回答1:


Here is what I use to get a longer expiring token

FacebookClient fbcl = new FacebookClient(atoken);
fbcl.AccessToken = //your short access token;
fbcl.AppId = //your app id;
fbcl.AppSecret = // your app secret;

//try to get longer token
try
{
    dynamic result= fbcl.Get("oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=fb_exchange_token&fb_exchange_token=" + atoken);
    atoken = result.access_token;
}
catch
{
    dynamic result= fbcl.Get("oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=fb_exchange_token&fb_exchange_token=" + atoken);
    atoken = result.access_token;
}

Sometimes this gives out an error like "Couldn't make secure SSL connection to FB" or sth like that. So I try it again in catch. Maybe you can solve this and help me too :) Cheers




回答2:


This works for me:

public static string RefreshAccessToken(string currentAccessToken)
{
        FacebookClient fbClient = new FacebookClient();
        Dictionary<string, object> fbParams = new Dictionary<string, object>();
        fbParams["client_id"] = "your app id";
        fbParams["grant_type"] = "fb_exchange_token";
        fbParams["client_secret"] = "your client secret";
        fbParams["fb_exchange_token"] = currentAccessToken;            
        JsonObject publishedResponse = fbClient.Get("/oauth/access_token", fbParams) as JsonObject;
        return publishedResponse["access_token"].ToString(); 
}



回答3:


I finally crack this problem by using the offline_access permission, you may first refer to this: http://operatorerror.org/2011/07/get-a-facebook-api-access-token-that-never-expires/ , you will know how to get a Facebook API Access Token that Never Expires.

Next, you may refer to this in case you run into the problem that the offline_access permission cannot be checked: offline_access permission not present in Graph api explorer in facebook graph api



来源:https://stackoverflow.com/questions/11068538/how-to-renew-facebook-access-token-using-its-c-sharp-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!