1

I am using Google API to allow my users to send emails from my website. Emails are sent very well but as strange as it can appear I have trouble getting the users' information and especially his email.

Here is the code I wrote:

$client = new Google_Client();
$client->addScope(Google_Service_Gmail::GMAIL_READONLY);
$client->addScope(Google_Service_Gmail::GMAIL_LABELS);
$client->addScope(Google_Service_Gmail::GMAIL_MODIFY);
$client->setAuthConfig(__DIR__.'/credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$accessTokenLive = $client->fetchAccessTokenWithRefreshToken($refreshToken);
$client->setAccessToken($accessTokenLive);
$oauth = new Google_Service_Oauth2($client);
$accessToken = $client->getAccessToken();
$userData = $oauth->userinfo->get();
var_dump($userData);

The $refreshToken is fetched from the database

I am getting an error message:

Request is missing the required authentication credential. Expected OAuth 2 access token, login cookie or another valid authentication credential.

I understand $oauth->userinfo->get() may need OAuth credentials but I can't find where to get them and most answers concerning the same issues comes out with the script above. Plus Google's API documentation says the scope is right.

I really struggle to understand how it works.

BabaD
  • 23
  • 4
  • 2
    Possible duplicate of [Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential](https://stackoverflow.com/questions/46545966/request-is-missing-required-authentication-credential-expected-oauth-2-access-t) – nbk Sep 13 '19 at 23:25
  • I didn't find any answer corresponding to my problem on the link above – BabaD Sep 16 '19 at 07:51
  • then read the googke site how to get an key https://developers.google.com/identity/protocols/OAuth2 – nbk Sep 16 '19 at 11:15
  • I have those developer keys. It's stored in the "credentials.json" file. I have access and refresh token. My script allows me to send mails, I just can't access users' infos. – BabaD Sep 16 '19 at 21:54
  • I thinkl reading this here https://stackoverflow.com/questions/14007560/get-userinfo-from-google-oauth-2-0-php-api/23146458#23146458 that you are mising a $client->setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile') – nbk Sep 16 '19 at 22:08

0 Answers0