我在网页上实现了一个按钮“在Google+上登录”,除了我无法获取用户的电子邮件地址,一切正常。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)]
保护 'internal_gapi_mappings'= > 阵列 (大小= 3)
- 'familyName'= >字符串 'FAMILY_NAME' (长度= 11)
- '给定名称'= >字符串 'GIVEN_NAME' (长度= 10)
- 'verifiedEmail'= >串'verified_email' (长度= 14)
公共 '电子邮件'= >空
公共 'familyName'= >字符串> 'SomeFamilyName' (长度= 6)
公共 '性别'= >串 '男性' (长度= 4)
公共 '给定名称'= >字符串 'SomeFirstName' (长度= 4)
公共 'HD' = >空
公共的 'id'= >字符串 '1021714674404 ********' (长度= 21)
公共 '链接'= >字符串 'https://plus.google.com/1021714674404********' (长度= 45)
公共 '区域设置'= >串成 'en' (长度= 2)
公共 '姓名'= >字符串 'SomeFirstName SomeFamilyName' (长度= 11)
公共 '画面'= >串的“https://lh6.googleusercontent。COM/someUrl /” (长度= 92)
公共 'verifiedEmail'= >空
- 保护 'modelData'= >
---- 阵列 (大小= 2)
------ 'GIVEN_NAME'= >字符串 'SomeFirstName' (长度= 4)
------ 'FAMILY_NAME'= >字符串 'SomeFamilyName' (长度= 6)
- 保护 '处理'= >
---- 阵列 (大小= 0)
------ 空
正如你可以看到,我的电子邮件地址是NULL,我真的需要至少获得电子邮件地址(famillyName和firstname,但我有这些)。
在beggining我用Google_Service_Plus对象,但我有同样的结果(电子邮件空),我发现这个线程(Google Apps SSO - First and Last Name with OAuth 2 without using Google+)目前的解决方案,但它仍然没有工作 我也发现这个线程(Using Google+ API for PHP -- Need to get the users email),但似乎现在将apiOauth2Service称为Google_Service_Oauth2,因此它基本相同。
奇怪的是,当我使用谷歌自己的例子(https://developers.google.com/apis-explorer/#p/plus/v1/plus.people.get),我尝试用我的用户ID得到我的电子邮件地址,它返回一个空数组(它解释了我的代码中的空值)。
200个OK
- 隐藏标题 -
的Cache-Control:私人,最大年龄= 0,必重新验证,无转换 内容编码:gzip 内容 - 长度:23 内容类型:application/json;字符集= UTF-8 日期:星期一,2014年11月24日17时34分39秒GMT 的Etag: “RqKWnRU4WW46-6W3rWhLR9iFZQM/C5t5kUwLc-tXgfm2-1GGe31qPDs” 到期:星期一,2014年11月24日17时34分39秒GMT 服务器: GSE
{}
我也试过这个解决方案(Google Api get users Email Address),但它也不起作用。
但是,当我尝试使用我的Google帐户注册到此网站(https://www.airbnb.fr/)时,他们得到了我的电子邮件地址!所以这是可能的,我只是不知道如何。
在此先感谢您的帮助:)。
不幸的是不断变化的范围,因为它来自于redirectUri不能解决问题 – 2014-12-01 10:03:09