Lenny
Lenny

Reputation: 85

How to renew Facebook access token using its C# SDK

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.

Upvotes: 3

Views: 4805

Answers (3)

Aswath Krishnan
Aswath Krishnan

Reputation: 1181

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

Upvotes: 3

Emre
Emre

Reputation: 388

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

Upvotes: 3

Lenny
Lenny

Reputation: 85

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

Upvotes: -3

Related Questions