问题
I use Facebook C# SDK v 6 and enabled "Remove Offline Access" on my application settings and after login and get the access token, I am trying to exchange for long lived token(60days one) I am unable to get it as both tokens expiration is with in 24 hrs.
Here is my code
For log in to 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
});
To get short lived access token
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"
});
To get long lived access 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
});
回答1:
This will do.
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;
}
来源:https://stackoverflow.com/questions/11935782/unable-to-get-long-lived-expiration-token