Search code examples
facebookexceptionfacebook-oauth

Facebook OAuthException when offline_access token is invalid


I know that even the offline_access token got from Facebook can be invalid (in case the user changed password for example). In that case I need to catch the thrown exception and offer the user of my site to authorize again.

So what is the exact error code/message thrown in this case? I know it's an OAuthException, but wasn't able to get the code. Can't use just the type, as there are many other OAuthException-s.

My plan is: change the base_facebook.php and add the case handle-ing code here:

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
        /*
         *   Need to add an appropriate case here.
        */

      case 'invalid_token':
        $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;
  }

Solution

  • In case anyone needs it, I found the answer somehow. The Exception for those kind of OAuthException is 190. Can be got from thrown exception like this:

    $e->getCode();