Google Api get users Email Address

2019-03-02 16:14发布

问题:

I am using the Google Api PHP client to Log the user in. I want to get the Users Email address. I have the following code.

Scope:

$client->setScopes('https://www.googleapis.com/auth/userinfo.profile');

Code:

$request = new apiHttpRequest("https://www.googleapis.com/oauth2/v2/userinfo?alt=json");
$userinfo = $client->getIo()->authenticatedRequest($request);

$response = $userinfo->getResponseBody();
print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";

This give me only the following details

Array
(
    [id] => 110084312800396764
    [name] => Harsha M V
    [given_name] => Harsha M
    [family_name] => V
    [picture] => https://lh6.googleusercontent.com/-Xusc8lwgLIQ/AAAAAAAAAAI/AAAAAAAAAAA/sOthy23uJGk/photo.jpg
    [locale] => en
)

回答1:

Setting the scope in an array does the job.

$this->client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));


回答2:

https://www.google.com/accounts/AuthSubRequest?next=http://www.example.com/index.php&scope=https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/analytics.readonly&secure=0&session=1 
or
https://www.google.com/accounts/AuthSubRequest?next=http://www.example.com/index.php&scope=email+https://www.googleapis.com/auth/analytics.readonly&secure=0&session=1

both of above links works for me in php