我正在使用Facebook C#SDK版本6.0.20来验证用户身份。C#SDK版本6.0.20 - (OAuthException - #190)验证访问令牌的错误
它工作正常,我可以得到用户的名字和姓氏。但是,当我试图在稍后的时间,我正在访问令牌,但fbClient.Get(“me”)
与下面的错误而失败:
(OAuthException – #190) Error validating access token: Session does not match current stored session. This may be because the user changed the password since the time the session was created or Facebook has changed the session for security reasons.
请帮助。
我运行下面的代码,当用户从身份验证对话框重定向:
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;
}
我将我的代码移到了实时环境中,现在看起来工作正常。谢谢您的帮助。 –