Error validating access token: The session is invalid because the user logged out
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?
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.
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.
What about the CanvasAnthorizer object ? Doesn't this object ensures that the user is logged and with the correct permissions ?
精彩评论