Error Validating Access Token

2020-04-05 07:17发布

问题:

I get this error occasionally:

Fatal error: Uncaught Exception: 190: Error validating access token: This may be because the user logged out or may be due to a system error. thrown in ..base_facebook.php on line 1053

The issue is that I get it at random times, without any warning. I can go weeks without seeing it, then all of a sudden it will come up. The solution? You can wait it out. It throws this error for about 15 minutes then you can refresh and it will allow you to continue with what you were doing.

My question is, is this an issue on my end? Or is this a Facebook issue? This is a Facebook connect page if that helps. I did research and found out it could be caused from the FQL query. But then wouldn't it be consistent at least? It seems like it is completely random. Here is my FQL query anyway just in case.

$facebook->api(array('method' => 'fql.query','query' => "SELECT first_name,middle_name,last_name,email,affiliations FROM user WHERE uid=".$user));

Here are lines 1052-1071 of Base_Facebook,

protected function throwAPIException($result) {
$e = new FacebookApiException($result);
switch ($e->getType()) {
  // OAuth 2.0 Draft 00 style
  case 'OAuthException':
    // OAuth 2.0 Draft 10 style
  case 'invalid_token':
    // REST server errors are just Exceptions
  case 'Exception':
    $message = $e->getMessage();
  if ((strpos($message, 'Error validating access token') !== false) ||
      (strpos($message, 'Invalid OAuth access token') !== false)) {
    $this->setAccessToken(null);
    $this->user = 0;
    $this->clearAllPersistentData();
  }
}

throw $e;

}

Thank you

回答1:

As written in How-To: Handle expired access tokens (link is broken) developers blog post

Access tokens for users can become invalid due to various reasons. In most cases, they can expire if it’s past the time specified by the expires field (by default access token have a 2 hour lifetime). What many developers do not realize is that an access token can also expire if a user changes her password, logs out or if she de-authorizes the app via the App Dashboard. It is very important that your apps handle such situations. If your access token expires, you need to reacquire a valid access token.

Before we could use offline_access permission to get token that not expire (unless user is connected with application), this permission is now deprecated, see Deprecation of Offline Access Permission to see how you can get access_token with longer expiration time.

Update:
As of Aug 2012 Facebook PHP-SDK have added simple way of extending access_token (see How to extend access token validity since offline_access deprecation for more details)

Update 2: Note that original blog-post from Facebook about expired tokens handling doesn't exists anymore. There is new documentation hewever that may be used to get the details. https://developers.facebook.com/docs/facebook-login/access-tokens/#extending