Search code examples
phpgoogle-analytics-api

Google Analytics API refresh token API Fatal Error


I'm using the Google Analytics API and am getting refresh tokens as explained in the documentation. I am however facing an error if a user has revoked access from their Google account but still tries to use my integration (which is actually a possibility). When I call:

$client->setAccessToken($newtoken);

To get a new token using the refresh token a fatal error is thrown and I cannot catch this error.

Ideally I would like to check the user's refresh token access, however I can't work out how to do this, is this possible with the php library?


Solution

  • as i can see from the code of the library you have to catch InvalidArgumentException

      public function setAccessToken($token)
      {
        if (is_string($token)) {
          if ($json = json_decode($token, true)) {
            $token = $json;
          } else {
            // assume $token is just the token string
            $token = array(
              'access_token' => $token,
            );
          }
        }
        if ($token == null) {
          throw new InvalidArgumentException('invalid json token');
        }
        if (!isset($token['access_token'])) {
          throw new InvalidArgumentException("Invalid token format");
        }
        $this->token = $token;
      }