1

我正在使用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; 
} 

回答

2

fbClient.Get(“我”)不工作(没有前面的“/”),这就是我们正在使用它正常工作。

string redirectUrl = "http://localhost:51215/TestFBWebSite/FacebookRedirect.aspx"; 

我注意到这部分 - 我发现,整合不起作用使用本地主机(或127.0.0.1),我们通过建立一个主机记录的解决了这个时候“test.mydomain.com”点到127.0.0.1

我们在现场环境中偶尔遇到#190错误。奇怪的是,一旦Facebook刚刚对用户进行了身份验证,应该只会触发产生错误的位置,因此身份验证令牌应该不会过期! 我们的编码来处理这个错误(即不认证用户到我们的系统。),但到目前为止,我能想到的是问题的根源的两种可能性是:

  1. 我们呼吁Facebook的过快获得令牌后 - 可能是一个可能性,如果在Facebook的复制延迟传递新的身份验证令牌给所有相关的服务器
  2. 通用内部错误在Facebook的

我认为这是可能的选项1,但是我还没有找到足够的信息来备份它

+0

我将我的代码移到了实时环境中,现在看起来工作正常。谢谢您的帮助。 –

1

我认为这是在开始时有语法错误。

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; 

试试这个代码使用“\我”,而不是“我”

+0

当我将我的代码移至实时环境时,“\ me”和“me”都可以正常工作。谢谢。 –

0

您的帐户将被屏蔽并显示190个错误代码如果您向朋友发送更多未知人的请求。因此取消请求并重新登录。

相关问题