【问题标题】:What is the simplest possible way to grab an email address from the PHP Google API从 PHP Google API 获取电子邮件地址的最简单方法是什么
【发布时间】:2014-07-23 01:25:22
【问题描述】:

我有一个用户的有效访问令牌,该用户使用他们的 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 有很多相互矛盾的建议,也许 google api 已经改变,但是最少的代码量可以让我获得他们的电子邮件地址?

【问题讨论】:

  • 你要求什么范围?

标签: php google-api google-plus google-api-php-client


【解决方案1】:

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

$oauth2 = new Google_Oauth2Service($client);
$userinfo = $oauth2->userinfo->get();
var_dump($userinfo);

echo "Email is: {$userinfo['email']}\n";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 2011-03-27
    • 1970-01-01
    • 2011-03-29
    • 2016-06-28
    相关资源
    最近更新 更多