3

我正在使用 Facebook C# SDK 从 Facebook 获取尽可能多的数据,例如帖子、评论、用户信息,但是我的程序在我的访问令牌在一段时间后过期后停止,我必须重新启动程序。我从Facebook 开发者工具中获得了访问令牌,但如何更新令牌?它是http://csharpsdk.org/docs/web/handling-expired-access-tokens中的 TOTO 。

4

3 回答 3

3

这对我有用:

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(); 
}
于 2012-11-16T04:40:43.280 回答
3

这是我用来获得更长过期令牌的方法

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

有时这会发出诸如“无法与 FB 建立安全 SSL 连接”之类的错误。所以我再次尝试捕捉。也许你可以解决这个问题并帮助我:) 干杯

于 2012-07-18T10:36:21.683 回答
-3

我终于通过offline_access权限破解了这个问题,你可以先参考这个:http: //operatorerror.org/2011/07/get-a-facebook-api-access-token-that-never-expires/ ,你将知道如何获取永不过期的 Facebook API 访问令牌。

接下来,如果您遇到无法检查offline_access权限的问题,您可以参考:offline_access permission not present in Graph api explorer in facebook graph api

于 2012-06-29T04:20:03.577 回答