我正在网页上实现一个"使用 Google+ 登录"按钮,除了我无法获取用户电子邮件地址之外,一切正常。
这是我的 HTML 代码
<div id="signinButton">
<span class="g-signin"
data-scope="https://www.googleapis.com/auth/plus.login"
data-clientid="{{ google.cid }}"
data-redirecturi="postmessage"
data-accesstype="offline"
data-cookiepolicy="single_host_origin"
data-callback="signInCallback">
</span>
</div>
当用户单击此按钮并接受与我的应用程序共享他的数据时,代码将在ajax中发送到服务器。这是应该获取用户数据的服务器部分。
$code = $request->getContent();
$client = new Google_Client();
$client->setClientId('Google_Api_Code');
$client->setClientSecret('Google_Api_Secret');
$client->setRedirectUri('postmessage');
$client->setScopes('email', 'profile');
$client->authenticate($code);
$oAuth = new Google_Service_Oauth2($client);
$oAttr = $oAuth->userinfo->get();
return new Response(var_dump($oAttr));
这是用户信息的var_dump
对象(Google_Service_Oauth2_Userinfoplus)[268]
保护"internal_gapi_mappings" => 数组(大小 = 3)
--'familyName' => 字符串 'family_name' (长度 = 11)
--'givenName' => 字符串 'given_name' (长度 = 10)
--'verifiedEmail' => 字符串 'verified_email' (长度 = 14)
公共"电子邮件" => 空
公共"familyName" => 字符串> "SomeFamilyName" (长度 = 6)
公共"性别" => 字符串 "男性" (长度 = 4)
公共'givenName' => 字符串 'SomeFirstName' (length=4)
公共'hd' => 空
公共'id' => 字符串 '1021714674404********'(长度 = 21)
公共'link' => 字符串 'https://plus.google.com/1021714674404********'(长度 = 45)
公共"区域设置" => 字符串 'en' (长度 = 2)
公共'name' => 字符串 'SomeFirstName SomeFamilyName' (length=11)
公共'picture' => 字符串 'https://lh6.googleusercontent.com/someUrl/' (长度 = 92)
公共"已验证电子邮件" => 空
--受保护的"模型数据" =>
----数组(大小 = 2)
------'given_name' => 字符串 'SomeFirstName' (长度 = 4)
------'family_name' => 字符串 'SomeFamilyName' (长度 = 6)
--受保护的"已处理" =>
----数组(大小 = 0)
------空
如您所见,我的电子邮件地址为空,我真的需要至少获得电子邮件地址(家族名称和名字,但我有这些)。
一开始我正在使用Google_Service_Plus对象,但我得到了相同的结果(电子邮件空),我在这个线程上找到了当前的解决方案(谷歌企业购 SSO - 使用 OAuth 2 的名字和姓氏,不使用 Google+),但它仍然不起作用我还发现了这个线程(使用 Google+ API for PHP - 需要获取用户电子邮件),但 apiOauth2Service 现在似乎被称为Google_Service_Oauth2所以它基本上是一样的。
奇怪的是,当我使用 Google 自己的示例 (https://developers.google.com/apis-explorer/#p/plus/v1/plus.people.get) 并尝试使用我的用户 ID 获取我的电子邮件地址时,它会返回一个空白数组(解释我的代码中的 null 值)。
200 OK
- 隐藏标题 -
缓存控制:私有、最大年龄=0、必须重新验证、无转换 内容编码:gzip 内容长度:23 内容类型:应用程序/json;字符集=UTF-8 日期:2014 年 11 月 24 日星期一 17:34:39 GMT Etag: "RqKWnRU4WW46-6W3rWhLR9iFZQM/C5t5kUwLc-tXgfm2-1GGe31qPDs" 到期: 周一, 24 十一月 2014 17:34:39 GMT 服务器:地面设备
{ }
我也尝试了这个解决方案(谷歌 API 获取用户电子邮件地址),但它也不起作用。
但是,当我尝试使用我的谷歌帐户注册到这个网站(https://www.airbnb.fr/)时,他们得到了我的电子邮件地址!所以有可能,我只是不知道怎么做。
提前感谢您的帮助:)。
所以我弄清楚了错误来自哪里。
在我的代码中,谷歌没有向我发送用户的电子邮件,因为我的应用程序没有经过身份验证。由于此身份验证问题,我只能访问用户的公共信息,而不能访问电子邮件。
此身份验证错误来自 HTML 中的这一行
data-redirecturi="postmessage"
当值"postmessage"用作redirect_uri时,您的应用程序将具有"guest"权限,并且Google不会抛出有关redirect_uri参数的任何错误。此值应仅在早期开发中使用,以测试 Google 的 API 与您的应用程序之间的通信。
以下是有关redirect_uri参数的官方文档:
此参数的值必须与 Google 开发者控制台的"凭据"页面中显示的值之一完全匹配(包括 http 或 https 方案、大小写和尾部斜杠)
但是,我无法使用谷歌javascript SDK使事情正常工作,如上所示。我使用了一个基本链接:
<a href="https://accounts.google.com/o/oauth2/auth?redirect_uri=http://www.example.com/googleplus/&response_type=code&client_id={{ google.cid }}&scope=https://www.googleapis.com/auth/plus.login+https://www.googleapis.com/auth/userinfo.email&access_type=offline" >
Connect with Google+
</a>
oAuth Playground帮助我解决了这个问题。
希望它能帮助某人:)
您还需要在 html 中设置电子邮件的范围,更多信息电子邮件范围:
<div id="signinButton">
<span class="g-signin"
data-scope="https://www.googleapis.com/auth/plus.login email"
...
data-callback="signInCallback">
</span>
</div>
或其他选项:
<div id="signinButton">
<span class="g-signin"
data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read"
...
data-callback="signInCallback">
</span>
</div>
转到Google/model.php张歌
protected $modelData = array();
to
public $modelData = array();