Google Api get users Email Address

守給你的承諾、 提交于 2019-12-20 05:32:10

问题


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



来源:https://stackoverflow.com/questions/11012142/google-api-get-users-email-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!