1

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.

My code:

public function gmail_get_messages()
{
    $client = new Google_Client();

    $client->setApplicationName("Gmail API test");
    $client->setDeveloperKey("MY_KEY");
    $client->setClientSecret('MY_CLIENT_SECRET');
    $client->setScopes(array('https://www.googleapis.com/auth/gmail.readonly'));

    // $client->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);
}

Response:

{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }

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.

How do I properly pass the tokens to the request? Is the only thing missing or there is something else?

If someone can provide an example of a properly request I would be very grateful!

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
FrancescoMussi
  • 20,760
  • 39
  • 126
  • 178
  • Shouldn't you have just edited your other question? http://stackoverflow.com/questions/30613152/gmail-api-how-to-get-access-token why do you need a new one? – Linda Lawton - DaImTo Jun 03 '15 at 12:25
  • 1
    I have edited that question indeed and answered too. I was finally able to get the tokens in a json file. I am now trying to include the tokens to my request to the API to make it working. – FrancescoMussi Jun 03 '15 at 12:29

1 Answers1

1

I think you are close! Try assigning the access token to the client like this:

$client = new Google_Client();


$client->setApplicationName('Gmail API test');
$client->setDeveloperKey('MY_KEY');
$client->setClientSecret('MY_CLIENT_SECRET');
$client->SetClientId('MY_CLIENT_ID');
$client->setScopes(array('https://www.googleapis.com/auth/gmail.readonly'));
$client->setAccessToken('{"access_token":"MY_ACCESS_TOKEN",
                          "token_type":"Bearer"‌​,"expires_in":3600,
                          "refresh_token":"MY_REFRESH TOKEN","created":1433329214}');

$service = new Google_Service_Gmail($client);

$messages = $service->users_messages->listUsersMessages('me');
$list = $messages->getMessages();

// Look at the contents of the first message
$message = $list[0];
$parts = $message->getPayload()->getParts();

$body = $parts[0]['body'];
$rawData = $body->data;
$sanitizedData = strtr($rawData,'-_', '+/');
$decodedMessage = base64_decode($sanitizedData);
var_dump($decodedMessage);
Tholle
  • 108,070
  • 19
  • 198
  • 189
  • Is not working yet. I am getting the same error. Do you have perhaps a full example of a working request? – FrancescoMussi Jun 04 '15 at 06:00
  • Thank you for the efforts! I am getting this error: Uncaught exception 'Google_Auth_Exception' with message 'Could not json decode the token'. Can i pass the token as an external json file? Probably once done that everything will work fine. – FrancescoMussi Jun 04 '15 at 07:06
  • @johnnyfittizio Try the latest edit! I'm not sure how to get it from file though :( – Tholle Jun 04 '15 at 07:53
  • I have tried it. I think yours is more proper code. But unfortunately there is still this issue about to pass the token – FrancescoMussi Jun 04 '15 at 07:59
  • @johnnyfittizio Did you try the subtle change? :) $client->setAccessToken('{"access_token":"MY_ACCESS_TOKEN"}'); – Tholle Jun 04 '15 at 08:02
  • Ok i have made two changes to that code and now is working. I get an array with threadId and other parameters that i suppose is the right thing. If you make these two edit i will give correct answer to you that help me to get there. Change 1: Include the whole gmail-api-quickstart.json in the access token. $client->setAccessToken('{"access_token":"MY_ACCESS_TOKEN","token_type":"Bearer","expires_in":3600,"refresh_token":"MY_REFRESH TOKEN","created":1433329214}'); – FrancescoMussi Jun 04 '15 at 08:13
  • Change 2: Add the client ID to the settings. $client->SetClientId('MY_CLIENT_ID'); – FrancescoMussi Jun 04 '15 at 08:14