1

我使用 Facebook C# SDK v 6 并在我的应用程序设置中启用了“删除离线访问”,在登录并获取访问令牌后,我正在尝试交换长寿命令牌(一个 60 天)我无法获得它,因为两个令牌都过期了24 小时内。

这是我的代码

登录 Facebook

private const string Scope = "publish_stream,manage_pages";
   FacebookClient _fb = new FacebookClient();
   var fbLoginUrl = _fb.GetLoginUrl(
                new
                {
                    client_id = AppId,
                    client_secret = Appsecret,
                    redirect_uri = RedirectUri,
                    response_type = "code",
                    scope = Scope,
                    state = state
                });

获取短期访问令牌

if (Request.QueryString["code"] != null)
            code = Request.QueryString["code"];
           var result = _fb.Post("oauth/access_token",
                                  new
                                  {
                                      client_id = AppId,
                                      client_secret = Appsecret,
                                      redirect_uri = RedirectUri,
                                      code = code,
                                      scope = Scope,
                                      response_type="token"
                                  });

获取长寿命访问令牌

       var  result1 = _fb.Post("oauth/access_token",
                                  new
                                  {
                                      client_id = AppId,
                                      client_secret = Appsecret,
                                      grant_type = "fb_exchange_token",
                                      fb_exchange_token= Session["fb_access_token"] as string
                                  });
4

1 回答 1

1

这会做。

var result = GetExtendedAccessToken("my_short_lived_old_token");
var extendedToken = result.access_token;

public dynamic GetExtendedAccessToken(string oldToken)
{
    dynamic result = new ExpandoObject();
    try
    {
        var api = new FacebookClient
        {
            AccessToken = oldToken,
            AppId = ClientID,
            AppSecret = ClientSecret
        };
        dynamic parameters = new ExpandoObject();
        parameters.grant_type = "fb_exchange_token";
        parameters.fb_exchange_token = oldToken;
        parameters.client_id = ClientID;
        parameters.client_secret = ClientSecret;
        result = api.Get("oauth/access_token", parameters);
    }
    catch (FacebookOAuthException err)
    {
        result.error = "Error";
        result.message = err.Message;
    }
    catch (Exception err)
    {
        result.error = "Error";
        result.message = err.Message;
    }
    return result;
}
于 2012-08-17T11:43:53.140 回答