我正在使用Google Api PHP客户端登录用户。我想获取用户电子邮件地址。我有以下代码。
范围:
$client->setScopes('https://www.googleapis.com/auth/userinfo.profile');
代码:
$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>";
这只给我以下细节
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 )
答案 0 :(得分:4)
在数组中设置范围可以完成工作。
$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'));
答案 1 :(得分:1)
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
以上两个链接都适用于我的PHP