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.