Php 如何检索用户';谷歌的电子邮件

Php 如何检索用户';谷歌的电子邮件,php,oauth,google-api,google-oauth,Php,Oauth,Google Api,Google Oauth,我正在使用PHP中的Google客户端库。 我已成功通过身份验证。 缺少一件简单的事情(我添加了正确的范围)。如何检索 在我完成身份验证过程后用户的电子邮件。 以下是我所拥有的: $client = new Google_Client(); $client->setClientId(MYCLIENTID); $client->setClientSecret(MYSECRET); $client->setRedirectUri(SOMEURLINMYSYSTEM); $servi

我正在使用PHP中的Google客户端库。
我已成功通过身份验证。
缺少一件简单的事情(我添加了正确的范围)。如何检索 在我完成身份验证过程后用户的电子邮件。
以下是我所拥有的:

$client = new Google_Client();
$client->setClientId(MYCLIENTID);
$client->setClientSecret(MYSECRET);
$client->setRedirectUri(SOMEURLINMYSYSTEM);
$service = new Google_Service_Oauth2($client);
$client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
$client->authenticate($_GET['code']);//I have the right code, and I am being authenticated

//TODO Get from google the user's email ?????????
我在这里使用PHP库:

哎呀,刚刚找到它:

$client = new Google_Client();
$client->setClientId(MYCLIENTID);
$client->setClientSecret(MYSECRET);
$client->setRedirectUri(SOMEURLINMYSYSTEM);
$service = new Google_Service_Oauth2($client);
$client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
$client->authenticate($_GET['code']);//I have the right code, and I am being authenticated

$client->authenticate($code);
$plus = new Google_Service_Plus($client);
$person = $plus->people->get('me');
var_dump($person);

更简单的是,您可以这样做:

$user = $service->userinfo->get();

echo $user->name;
echo $user->id;
echo $user->email;
echo $user->link;
echo $user->picture;
要使其工作,也不要忘记作用域:

$client->addScope('https://www.googleapis.com/auth/userinfo.email');
$client->addScope('https://www.googleapis.com/auth/userinfo.profile');
// $client->addScope(Google_Service_Plus::PLUS_ME);

谷歌OAuth 2-获取用户电子邮件

$client = new Google_Client();
$client->addScope(Google_Service_Plus::USERINFO_EMAIL);
$client->addScope(Google_Service_Plus::USERINFO_PROFILE);

$tokeninfo = $client->verifyIdToken();
echo $tokeninfo->name;
echo $tokeninfo->email;
更新:似乎只有在有效的“访问令牌”(首次登录)期间才可用


如果你有setAccessType(“脱机”)和setApprovalPrompt(“自动”),那么“name”属性以后将不可用,你需要像上面的帖子中提到的那样使用Google Plus。

?看起来,是的,我的问题更清楚,所以我将把它保留在我找不到的地方。
Google_Service_Oauth2
是正确使用的服务。。必须在代码中找到它。你有没有在某个地方找到一个概述,或者在代码中也搜索过?一年多以前……我很确定我或者在他们的开发者网站上找到了一些示例,或者在一些软件中找到了一些示例。请注意,这可能是当前库的旧版本,API可能已更改。这可以解释为什么你没有在文档中看到它。这里的$service变量是什么?@kewlashu请仔细查看原始问题,你会在那里找到变量。。。
$client = new Google_Client();
$client->addScope(Google_Service_Plus::USERINFO_EMAIL);
$client->addScope(Google_Service_Plus::USERINFO_PROFILE);

$tokeninfo = $client->verifyIdToken();
echo $tokeninfo->name;
echo $tokeninfo->email;