title: PHP Fatal error: Uncaught InvalidArgumentException: Invalid token format in vendor/google/apiclient/src/Google/Client.php:423
body: <p>Below code used for the edit google spreadsheet, but we are getting issue related to the access token, let me know if anyone has a solution for this issue. For the Outh client id we have flow these instructions:
  <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/web-php" rel="nofollow noreferrer">https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/web-php</a></p>
  
  <p><code>[29-Sep-2019 04:10:36 UTC] PHP Fatal error:  Uncaught InvalidArgumentException: Invalid token format in /home/help4dl9/public_html/doamin.com/wp-content/themes/twentynineteen/google-sheet/vendor/google/apiclient/src/Google/Client.php:423</code></p>
  
  <p>Here getClient function:</p>
  
  <pre><code>
  function getClient()
  {
  
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  
  $client = new Google_Client();
  
  $client-&gt;setApplicationName('APP NAME');
  
  $client-&gt;setScopes( 'https://www.googleapis.com/auth/spreadsheets' );
  
  $client-&gt;setAuthConfig( __DIR__ . '/credentials.json' );
  
  $client-&gt;setAccessType('offline');
  
  $client-&gt;setPrompt('select_account consent');
  
  // Load previously authorized token from a file, if it exists.
  // The file token.json stores the user's access and refresh tokens, and is
  // created automatically when the authorization flow completes for the first
  // time.
  $tokenPath = __DIR__ . '/token.json';
  if (file_exists($tokenPath)) {
      $accessToken = json_decode(file_get_contents($tokenPath), true);
      $client-&gt;setAccessToken($accessToken);
  }
  // If there is no previous token or it's expired.
  if ($client-&gt;isAccessTokenExpired()) {
      // Refresh the token if possible, else fetch a new one.
      if ($client-&gt;getRefreshToken()) {
          $client-&gt;fetchAccessTokenWithRefreshToken($client-&gt;getRefreshToken());
          $authObj = $client-&gt;getAccessToken();
          if(array_key_exists("access_token",$authObj)) {
              update_field( 'authentication_code', $authObj['access_token'],'option' ); // Save Token in Theme Option
          }
      } else {
          // Request authorization from the user.
          $authCode = get_field('authentication_code','option');
          // Exchange authorization code for an access token.
          $accessToken = $client-&gt;fetchAccessTokenWithAuthCode($authCode);
          $client-&gt;setAccessToken($accessToken);
          // Check to see if there was an error.
          if (array_key_exists('error', $accessToken)) {
              throw new Exception(join(', ', $accessToken));
          }
      }
      // Save the token to a file.
      if (!file_exists(dirname($tokenPath))) {
          mkdir(dirname($tokenPath), 0700, true);
      }
      file_put_contents($tokenPath, json_encode($client-&gt;getAccessToken()));
  }
  return $client;
  }```
  
  
  
  </code></pre>