我正在尝试扩展我的 facebook 访问令牌,以下是我所做的。但是,我无法延长令牌,当我检查 facebook accesstoken 调试器时,它仍然显示令牌将在一小时内到期。
有任何想法吗?
private string appId = "my app id";
private string appSecret = "my app secret";
private string returnUrl = "http://localhost:1868/callback";
private string _scope = "user_about_me,publish_stream,user_videos,user_photos";
[Test]
public void GetFacebookLoginUrl()
{
var fb = new FacebookClient();
dynamic result = fb.GetLoginUrl(
new
{
client_id = appId,
client_secret = appSecret,
redirect_uri = returnUrl,
response_type = "code",
scope = _scope
}
);
}
[Test]
public void exchangeCodeForAccessToken()
{
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = appId,
client_secret = appSecret,
redirect_uri = returnUrl,
code = "got the code after user accepts the request"
});
}
[Test]
public void ExtendExpiryTimeOfAccessToken()
{
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = appId,
client_secret = appSecret,
grant_type = "fb_exchange_token",
fb_exchange_token = "token from preview method"
});
}
解决方案
从用户帐户中取消对应用程序的授权后,一切都开始变得有意义。需要注意的一件事是,上面的代码实际上得到了一个“长寿命”的访问令牌,有效期为 60 天,并且不能延长。
如果使用Facebook 客户端身份验证,则可以使用“ExtendExpiryTimeOfAccessToken”示例对其进行扩展。