我正在asp.net中创建一个应用程序,该应用程序将使用其api的用户的facebook数据, 但是为了访问Fb api,我们需要首先执行OAuth(2.0)。 我也是通过使用以下代码中的DotNetOpenAuth库来实现的:
private static readonly FacebookClient client = new FacebookClient
{
ClientIdentifier = ConfigurationManager.AppSettings["fbAppID"],
ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["fbSecret"]),
};
protected void Page_Load(object sender, EventArgs e)
{
IAuthorizationState authorization = client.ProcessUserAuthorization();
if (authorization == null)
{
// Kick off authorization request
client.RequestUserAuthorization();
}
else
{
var req = WebRequest.Create("https://graph.facebook.com/oauth/access_token?client_id=" + ConfigurationManager.AppSettings["facebookAppID"] + "&client_secret=" + ConfigurationManager.AppSettings["facebookAppSecret"] + "&grant_type=fb_exchange_token&fb_exchange_token=" + Uri.EscapeDataString(authorization.AccessToken));
var request = WebRequest.Create("https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken));
using (var response = request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
var graph = FacebookGraph.Deserialize(responseStream);
this.nameLabel.Text = HttpUtility.HtmlEncode(graph.Name);
}
}
}
}
我的问题是,如何使用DotNetOpenAuth库获取facebook的长期访问令牌,以便用户不需要一次又一次地登录,我可以将其存储在某个地方。 请帮帮我。
答案 0 :(得分:0)
如果可能,请使用Facebook SDK for .Net长时间创建访问令牌。可能是60天。
使用Facebook;
var cl = new FacebookClient(short_lived_access_token);
dynamic result = client.Post("oauth/access_token", new
{
client_id = "your app id",
client_secret = "your app secret",
grant_type = "fb_exchange_token",
fb_exchange_token = client.AccessToken
});
var long_lived_access_token = result.access_token;