从PHP Google API获取电子邮件地址的最简单方法是什么?


What is the simplest possible way to grab an email address from the PHP Google API

对于使用Google ID登录到我的网站的用户,我有一个有效的访问令牌,并且我已经能够通过使用

获取他们的名字和头像:
$client->setAccessToken($response);
$plus = new Google_Service_Plus($client);
$me = $plus->people->get("me");
var_dump($me);

然而,这并没有给我他们的电子邮件。所以我试试:

$plus2 = new Google_Oauth2($client);
$email = $plus2->getuserinfo("me");
var_dump($email);

不起作用。它说Google_Oauth2不是一个类,这说得通,因为我基本上是在猜测,但什么能行得通呢?关于SO有很多相互矛盾的建议,也许谷歌api已经改变了,但最简单的代码量是什么,可以让我得到他们的电子邮件地址?

您不需要使用Plus API,只需调用$oauth2->userinfo->get()以获得包含电子邮件地址的属性的关联数组。例子:

$oauth2 = new Google_Oauth2Service($client);
$userinfo = $oauth2->userinfo->get();
var_dump($userinfo);
echo "Email is: {$userinfo['email']}'n";