How to renew Facebook access token using its C# SDK

删除回忆录丶 提交于 2019-12-05 21:40:21

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

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

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

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