3

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Lenny
  • 85
  • 1
  • 7
  • If you're using app token to access data, have a look at [this](http://stackoverflow.com/questions/10006708/getting-fb-page-data-from-facebook-using-c-sharp/10007058#10007058) – Mehmet Osmanoglu Jun 18 '12 at 20:29

3 Answers3

3

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

Emre
  • 388
  • 3
  • 10
3

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(); 
}
Aswath Krishnan
  • 1,131
  • 1
  • 9
  • 16
-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

Community
  • 1
  • 1
Lenny
  • 85
  • 1
  • 7
  • 2
    __offline_access is deprecated and will be removed soon__; building app functionality based on it _now_ is not a good idea at all. https://developers.facebook.com/roadmap/offline-access-removal/ – CBroe Jun 29 '12 at 08:52