How to renew Facebook access token using its C# SDK

Thanks for the great Facebook C# SDK which eases my work dealing with Facebook API. I'm using the sdk to fetch as much data, eg 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. The access token is got from https://developers.facebook.com/tools/access_token/, but how can I renew the token? It is TOTO in http://csharpsdk.org/docs/web/handling-expired-access-tokens. Could anyone please help around this problem? Thank you very much.


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


这适用于我:

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(); 
}

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

链接地址: http://www.djcxy.com/p/61608.html

上一篇: 通过更新实现访问令牌到期日

下一篇: 如何使用其C#SDK续订Facebook访问令牌