我正在使用 Facebook C# SDK 6.0.20 版对用户进行身份验证。
它对我来说很好,我能够获得用户的名字和姓氏。但是当我稍后尝试时,我得到了访问令牌,但fbClient.Get(“me”)
失败并出现以下错误:
(OAuthException – #190) 验证访问令牌时出错:会话与当前存储的会话不匹配。这可能是因为用户在创建会话后更改了密码,或者 Facebook 出于安全原因更改了会话。
请帮忙。
当用户从身份验证对话框重定向回来时,我运行以下代码:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["code"] != null)
{
Facebook.FacebookClient fbClient = new Facebook.FacebookClient(GetAccessToken());
dynamic me = fbClient.Get("me");
string firstName = me.first_name;
string lastName = me.last_name;
string email = me.email;
string userID = me.id;
string gender = me.gender;
string dob = me.birthday;
string locale = me.locale;
string mStatus = me.relationship_status;
}
}
private string GetAccessToken()
{
if (HttpRuntime.Cache["access_token"] == null)
{
Dictionary<string, string> args = GetOauthTokens(Request.Params["code"]);
HttpRuntime.Cache.Insert("access_token", args["access_token"], null, DateTime.Now.AddMinutes(Convert.ToDouble(args["expires"])), TimeSpan.Zero);
}
return HttpRuntime.Cache["access_token"].ToString();
}
private Dictionary<string, string> GetOauthTokens(string code)
{
Dictionary<string, string> tokens = new Dictionary<string, string>();
string clientId = "xxxxxxxxxxxxxxxxxx";
string redirectUrl = "http://localhost:51215/TestFBWebSite/FacebookRedirect.aspx";
string clientSecret = "xxxxxxxxxxxxxxxxxxxxxx";
string url = string.Format("https://graph.facebook.com/oauth/access_token? client_id={0}&redirect_uri={1}&client_secret={2}&code={3}",
clientId, redirectUrl, clientSecret, code);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string retVal = reader.ReadToEnd();
foreach (string token in retVal.Split('&'))
{
tokens.Add(token.Substring(0, token.IndexOf("=")),
token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
}
}
return tokens;
}