I was able to Link Account using Google Auth v2 in Alexa Skill. And I am able to get Access Token. Now I want to get the Google Email address with which user has used to link account. So how can I get that Google Email Address?
Asked
Active
Viewed 156 times
-1
-
I recomend going though the Google People api, try the person.get method it will return the full profile infomation. The userinfo endpoint is not reliable and will only return a limited amount of information. – Linda Lawton - DaImTo Mar 12 '21 at 19:09
1 Answers
1
Use the Oauth2 Scope "https://www.googleapis.com/auth/userinfo.email".
Add the scope to your Google Client (Example with the Google Library for PHP):
$googleClient = new Google_Client();
// Your code here with Client ID, Secret, ...
$googleClient->addScope('https://www.googleapis.com/auth/userinfo.email');
That'll give you the E-Mail of the user.
if(isset($_GET['code'])) {
// Your code here, the login code is in $_GET['code']
/**
* Get access token
*/
$token = $googleClient->fetchAccessTokenWithAuthCode($_GET['code']);
$googleClient->setAccessToken($token['access_token']);
// Create Google Auth Service with the Google Client
$google_auth = new Google_Service_Oauth2($googleClient);
// Get the userinfo
$google_auth_info = $google_auth->userinfo->get();
// Get the E-Mail:
$email = $google_auth_info->email;
}
I think the part you're looking for is authinfo->get()->email
, but i added a bit more context so you see how to use this.
Don't forget to enable this scope also in your Google Developers Console: https://console.cloud.google.com/apis/credentials/consent -> Edit -> Step 2: Scopes

EinLinuus
- 625
- 5
- 16
-
Note: the userinfo endpoint returns a limited amount of information and not always all of it. A better option is to use the People api which you can use with the same email, profile scopes – Linda Lawton - DaImTo Mar 12 '21 at 19:09