title: Gmail API: example of a request to the API
body: <p>I am trying access the Gmail API with my application. I should have done properly all the setup and I am testing a request to the API, in the specific the request to get the messages as read only.</p>
  
  <p>My code:</p>
  
  <pre><code>public function gmail_get_messages()
  {
      $client = new Google_Client();
  
      $client-&gt;setApplicationName("Gmail API test");
      $client-&gt;setDeveloperKey("MY_KEY");
      $client-&gt;setClientSecret('MY_CLIENT_SECRET');
      $client-&gt;setScopes(array('https://www.googleapis.com/auth/gmail.readonly'));
  
      // $client-&gt;setAccessToken($token);
  
      $service = new Google_Service_Gmail($client);
  
      $url = 'https://www.googleapis.com/gmail/v1/users/MY_EMAIL/messages';
  
      $header = array("Authorization: access_token {MY_ACCESS_TOKEN}");
  
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
      curl_setopt($ch, CURLOPT_ENCODING, "gzip");
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
  
      $retValue = curl_exec($ch);
      $response = json_decode(curl_exec($ch));
      $ee       = curl_getinfo($ch);
      print_r($ee);
  
      print_r($retValue);
  }
  </code></pre>
  
  <p>Response:</p>
  
  <pre><code>{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }
  </code></pre>
  
  <p>One main point regards the tokens.  I have obtain the access token and the refresh token as a file in json format, but I don't know how to include them in the request.</p>
  
  <p>How do I properly pass the tokens to the request?  Is the only thing missing or there is something else?  </p>
  
  <p>If someone can provide an example of a properly request I would be very grateful!</p>