Error validating access token: The session is invalid because the user logged out

青春壹個敷衍的年華 提交于 2019-12-12 19:20:40

问题


My code:

 var fbWebContext = FacebookWebContext.Current;
 if (fbWebContext.IsAuthorized())
 {
      var fb = new FacebookWebClient(fbWebContext);
      dynamic result = fb.Get("/me");

      name.Text = result.name;
 }

 dynamic result = fb.Get("/me");

throws:

(OAuthException) Error validating access token: The session is invalid because the user logged out.

How should I change my code to prevent this?


回答1:


You can ask for the "offline_access" extended permission. This will prevent this message and the user does not have to log in each time.




回答2:


You really can't prevent it, you just need to catch the exception and deal with it. Well Ok you could prevent it I guess on the client side by doing some equivalent calls from the FB Javascript SDK to detect a bad session (expired, logged off, user changed their password, etc.) and get them to log in again on the client before your server-side code is invoked.

If you look at the Facebook C# SDK source code you can see that IsAuthorized() = IsAuthenticated(). All IsAuthenticated does is check to see if you have a Session, and that the Session's expiration time hasn't passed. The Session could have been invalidated by the user logging off unbeknownst to the server.

There are other Facebook OAuth exceptions you can get here too. It is best to wrap the above code in a try/catch and deal with the exceptions by re-prompting the user to log in and get a fresh session with them.




回答3:


What about the CanvasAnthorizer object ? Doesn't this object ensures that the user is logged and with the correct permissions ?



来源:https://stackoverflow.com/questions/6167194/error-validating-access-token-the-session-is-invalid-because-the-user-logged-ou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!