1
votes

I am trying to get the list of my gmail messages using Google API. I manage to get my contact list and some other Google resources using this curl method but I get "invalid credentials" whenever I try to retrieve my email list. - Note that my scopes are set correctly.

Here's the php code:

    <?

$thetokenarray=array();
$thetokenarray= json_decode($_SESSION["token"] ,true);
$thetoken =  $thetokenarray["access_token"] ;

$thedomain =  substr(strrchr($_SESSION["email"], "@"), 1);

$ch = curl_init();
$headers = array("Authorization: access_token ".$thetoken."");
print_r($headers);

curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/gmail/v1/users/enattee%40gmail.com/messages');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);

$response = json_decode($response,true);
print_r($response);

?>

Here's Google Answer:

Array ( [error] => Array ( [errors] => Array ( [0] => Array ( [domain] => global [reason] => authError [message] => Invalid Credentials [locationType] => header [location] => Authorization ) ) [code] => 401 [message] => Invalid Credentials ) )

And here's is my token feedback from google when using https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=ya29.4gE6OJdoxxxT1UP0Yxxxxy3DvlPphF-1CVIFPfqp0j9Cuhm-hT0W_Qy3oGrcF-

{
 "issued_to": "64125009955-1hlmj0jgi9j2hd4sxxxxxaem9c9qhqcf8f.apps.googleusercontent.com",
 "audience": "64125xxxx09955-1hlmj0jgixxxxx9c9qhqcf8f.apps.googleusercontent.com",
 "user_id": "10597422xxxx25084",
 "scope": "https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/gmail.readonly https://www.google.com/m8/feeds/ https://www.googleapis.com/auth/admin.directory.user.readonly https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/plus.moments.write https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/plus.profile.agerange.read https://www.googleapis.com/auth/plus.profile.language.read https://www.googleapis.com/auth/plus.circles.members.read",
 "expires_in": 3427,
 "email": "[email protected]",
 "verified_email": true,
 "access_type": "offline"
}

Eventually, if there's an easier and different way to retrieve a logged in user email list this would be great as well :)

1

1 Answers

0
votes

Actually the code was ok. The AuthUrl generator was set with the wrong parameters on my test server.

For those who want to use the code the Google-PHP-Api https://github.com/google/google-api-php-client is the one used in the example.