title: Wrong number of segments in token (OAuth Google Api)
body: <p>My end goal is to send email to myself via Google Gmail API.</p>
  
  <p>And here is my problem.</p>
  
  <p>When i'm getting my access token an error pops up</p>
  
  <pre><code> Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Wrong number of segments in token: '
  </code></pre>
  
  <p>I read here <a href="https://stackoverflow.com/questions/15410013/cloud-endpoints-oauth2-error">Cloud endpoints oauth2 error</a> that <em>"This DOES NOT mean your token was invalid"</em>, but i'm getting a fatal error that interrupting my script.</p>
  
  <p>My access token looks like this <em>4/MqiIIl5K4S3D4iiieHshQt5D4M79oo07SbhMn22oe2o.cswa8t9ZuDAfJvIeHux6iLYXpNQmlAI</em></p>
  
  <p>If I refresh the page with this token i'd get another error, which is </p>
  
  <pre><code>'Error fetching OAuth2 access token, message: 'invalid_grant: Invalid code.'
  </code></pre>
  
  <p>Here is my code </p>
  
  <pre><code>&lt;?php
  
  include_once "templates/base.php";
  echo pageHeader("Simple API Access");
  
  require_once realpath(dirname(__FILE__) . '/../autoload.php');
  
  
  $client = new Google_Client();
  
  
  $client_id = '114600397795-j5sn0gvsdrup0s8dcmsj49iojp3m9biu.apps.googleusercontent.com';
  $client_secret = 'm3Dzankql_rs1OGICsA3Hbtc';
  $redirect_uri = 'http://alupa.com/gmail/examples/simple-query.php';
  
  $client-&gt;setClientId($client_id);
  $client-&gt;setClientSecret($client_secret);
  $client-&gt;setRedirectUri($redirect_uri);
  
  $client-&gt;addScope("https://www.googleapis.com/auth/gmail.readonly");
  $client-&gt;addScope("https://mail.google.com/");
  
  $apiKey = "AIzaSyCWXxrTshKsotxEYNZZCXxdVXhLeku55cw"; 
  $client-&gt;setDeveloperKey($apiKey);
  
  
  if (isset($_GET['code'])) {
    $client-&gt;authenticate($_GET['code']);
    $_SESSION['access_token'] = $client-&gt;getAccessToken();
    //$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    //header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  }else{
  
  $client-&gt;setApplicationName("Client_Gmail_Examples");
  
  }
  
  
  if (isset($_SESSION['access_token']) &amp;&amp; $_SESSION['access_token']) {
    $client-&gt;setAccessToken($_SESSION['access_token']);
  } else {
    $authUrl = $client-&gt;createAuthUrl();
  }
  
  if ($client-&gt;getAccessToken()) {
    $_SESSION['access_token'] = $client-&gt;getAccessToken();
    $token_data = $client-&gt;verifyIdToken()-&gt;getAttributes();
  }
  
  
  ?&gt;
  &lt;div class="box"&gt;
    &lt;div class="request"&gt;
  &lt;?php
  if (isset($authUrl)) {
    echo "&lt;a class='login' href='" . $authUrl . "'&gt;Connect Me!&lt;/a&gt;";
  } else {
    echo "&lt;a class='logout' href='?logout'&gt;Logout&lt;/a&gt;";
  }
  ?&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>alupa.com is my local domain and I don't see any problems with that</p>
  
  <p>I'm using original library from google <a href="https://github.com/google/google-api-php-client" rel="noreferrer">https://github.com/google/google-api-php-client</a></p>