我正在网页上实施“使用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>
当用户点击此按钮&amp;接受与我的应用程序共享他的数据,代码以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));
这是userinfo
的var_dump对象( Google_Service_Oauth2_Userinfoplus 的)的 268 的]
受保护'internal_gapi_mappings'=&gt; 数组 (size = 3)
- 'familyName'=&gt;字符串'family_name'(长度= 11)
- 'givenName'=&gt;字符串'given_name'(长度= 10)
- 'verifiedEmail'=&gt;字符串'verified_email'(长度= 14)
公开'电子邮件'=&gt;空
公开'familyName'=&gt;字符串&gt; 'SomeFamilyName'(长度= 6)
公开'gender'=&gt;字符串'male'(length = 4)
public 'givenName'=&gt;字符串'SomeFirstName'(长度= 4)
公开'hd'=&gt;空
公开'id'=&gt;字符串'1021714674404 ********'(长度= 21)
公开'link'=&gt;字符串'https://plus.google.com/1021714674404********'(长度= 45)
公开'locale'=&gt; string'en'(length = 2)
public 'name'=&gt; string'SomeFirstName SomeFamilyName'(length = 11)
public 'picture'=&gt;字符串'https://lh6.googleusercontent.com/someUrl/'(长度= 92)
公开'verifiedEmail'=&gt;空
- protected 'modelData'=&gt;
---- 数组 (size = 2)
------'given_name'=&gt;字符串'SomeFirstName'(长度= 4)
------'family_name'=&gt;字符串'SomeFamilyName'(长度= 6)
- 受保护'已处理'=&gt;
---- 数组 (size = 0)
------的空的
正如您所看到的,我的电子邮件地址为NULL,我真的需要至少获取电子邮件地址(famillyName和firstname,但我有这些地址)。
开始时我正在使用Google_Service_Plus对象,但我得到了相同的结果(电子邮件为null),我在这个帖子(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,所以它基本相同。
奇怪的是,当我使用Google自己的示例(https://developers.google.com/apis-explorer/#p/plus/v1/plus.people.get)并尝试使用我的用户ID获取我的电子邮件地址时,它会返回一个空白数组(它解释了我的代码中的空值)。
200 OK
- 隐藏标题 -
Cache-Control:private,max-age = 0,must-revalidate,no-transform 内容编码:gzip 内容长度:23 Content-Type: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/)时,他们会收到我的电子邮件地址!所以有可能,我只是不知道如何。
先谢谢你的帮助:)。
答案 0 :(得分:1)
所以我想出了错误的来源。
在我的代码中,谷歌没有向我发送用户的电子邮件,因为我的应用程序未经过身份验证。由于这种身份验证问题,我只能访问用户的公开信息,但不能访问电子邮件。
此身份验证错误来自HTML
中的此行data-redirecturi="postmessage"
当值&#34; postmessage&#34;用作redirect_uri,你的应用程序将有#34; guest&#34;特权和谷歌不会抛出有关redirect_uri参数的任何错误。该值仅应用于早期开发,以测试google的api与您的应用程序之间的通信。
以下是关于redirect_uri参数的official documentation:
此参数的值必须与Google Developers Console中“凭据”页面中显示的值之一完全匹配(包括http或https方案,大小写和尾部斜杠)
但是,如上所示,我无法使用google 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帮助我解决了这个问题。
希望它能帮助某人:)
答案 1 :(得分:0)
您还需要在html中设置电子邮件的范围,更多信息email scopes:
<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>
答案 2 :(得分:0)
转到Google / model.php 浓淡
protected $modelData = array();
to
public $modelData = array();