0

好的,这是我的代码片段

$client = new Google_Client();
$client->setScopes(array('https://www.googleapis.com/auth/plus.me','https://www.googleapis.com/auth/userinfo.email'));
$plus = new Google_PlusService($client);

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
} else {
    $_SESSION['auth_token'] = $client->authenticate();
}

现在我得到了 $_SESSION['auth_token'] ,其中包含类似

{"access_token":"ya29.AHESblablablabalbalbalbagTUI4-MfZvwtb6A","token_type":"Bearer","expires_in":3600,"id_token":"blablablabalbalbalba.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiY2lkIjoiNzAzNTg5ODA3MjMxLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXpwIjoiNzAzNTg5ODA3MjMxLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXVkIjoiNzAzNTgblablablabalbalbalbaRlbnQuY29tIiwiaGQiOiJnZW9mbGV4LmNvbS5teSIsInZlcmlmaWVkX2VtYWlsIjoidHJ1ZSIsImVtYWlsX3blablablabalbalbalbaFzei00N25nN3VoLU5NUmh3IiwiYXRfaGFzaCI6IllEeC13UXN6LTQ3bmc3dWgtTk1SaHciLCJpYXQiOjEzNzQ1NjA1NDcsImV4cCI6MTM3NDU2NDQ0N30.ek1FX-pY8BdaI8sDhA0rwmiIDyBV6pHPvBi-l7BxDJCR-YoDQGFN8y-kA1xg-vmoReiVCwhvQhHK__jc-xFSLfJDLbwk-UGyRKu_O7ZCaSviP7WupexIYvLGCyvgLBdjG3K7UIcq0o3L15mQPvQJr9LA7325gwEOZ12Pc1lAHKw","refresh_token":"1\/Y7oilVvUUutfkKMDfblablablabalbalbalbaE","created":1374560847}

我的问题是,如何获取用户的电子邮件?我知道你可以打电话

https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token

但这是获取用户电子邮件的“官方”方式吗?因为从这里https://developers.google.com/accounts/docs/OAuth2Login#exchangecode它说

您的应用程序在向 Google 进行身份验证流程期间收到的访问令牌可用于获取有关用户的其他个人资料信息。请注意,Google 正在修改不同端点的行为以简化它们的使用。我们目前根据现有行为推荐以下内容。如果您使用 Google+ 登录,则应始终通过使用访问令牌向https://www.googleapis.com/plus/v1/people/me发送 HTTPS GET 请求来从 People API 检索用户个人资料信息以验证要求。

所以我想最好的方法是向 googleapis.com 执行 HTTPS GET 请求。但我不知道该怎么做。谁能带路?

4

1 回答 1

2

Create a Google_Oauth2Service object (put it under the $plus initialization), like:

$client = new Google_Client();
$plus = new Google_PlusService($client);
$oauth2 = new Google_Oauth2Service($client);

and after succesfully authenticating the client use:

$userInfo = $oauth2->userinfo->get();
$email = $userInfo['email'];
于 2013-07-23T06:41:41.957 回答