Google + API不返回电子邮件地址


Google plus API not return the Email address

我使用以下代码通过google plus登录获取用户详细信息。

$token = $this->session->userdata('access_token');
$client = new Google_Client(); 
$client->setAccessToken($token);
$response = $this->_gp_plus->people->get('me');

,但它只返回以下详细信息

[kind] => plus#person
[etag] => "RqKWnRU4WW46-6W3rWhLR9iFZQM/IyjIQXmlWZGNFFImOAvg7vCilO0"
[gender] => male
[objectType] => person
[id] => 116202429381449556139
[displayName] => Vijay Kumar
[name] => Array
        (
            [familyName] => Kumar
            [givenName] => sara
        )
[url] => https://plus.google.com/116202429381449556139
[image] => Array
        (
            [url] => https://lh5.googleusercontent.com/-ae5axUqF88I/AAAawAAAAAssAAAI/AAAAAAAAssACo/djlbpkT0Okc/photo.jpg?sz=50
            [isDefault] => 
        )
[isPlusUser] => 1
[circledByCount] => 57
[verified] => 

这里没有返回电子邮件地址。有人知道是什么问题吗?

我找到了通过Google+ API成功获取电子邮件地址的解决方案。

首先,我使用以下示例来获取客户端的权限url:

from oauth2client import client
flow = client.flow_from_clientsecrets(
    'client_secret.json',
    scope='https://www.googleapis.com/auth/plus.profile.emails.read',
    redirect_uri='YOUR_REDIRECT_URI')
auth_uri = flow.step1_get_authorize_url()
print auth_uri  

注意scope应该是

https://www.googleapis.com/auth/plus.profile.emails.read

代替

https://www.googleapis.com/auth/plus.me .

然后在我的app中,我请求

https://www.googleapis.com/plus/v1/people/me?access_token=ACCESS_TOKEN

或者只是像上面提到的问题那样调用API:

plus.people.get({ userId: 'me', auth: oauth2Client }, function(err, profile) {
    if (err) {
      console.log('An error occured', err);
    return;
  }
  console.log(profile);
});

最后,我收到了回信:

{ 
 "kind": "plus#person", 
 "etag": "'"...."", 
 "occupation": "Researcher and Developer, Student and Programmer", 
 "skills": "Programming && Web Design && Server Management", 
 "gender": "male", 
 "emails": [ { "value": "MY_EMAIL@gmail.com", "type": "account" } ],
.... 

几点:

  • 您使用的是什么范围,除非您请求email范围,否则您将不会收到电子邮件
  • 您使用的是哪种访问令牌?一个简单的API密钥不能检索电子邮件地址,你必须使用Sign-in。

对于Sign-in,您将按照Google Sign-in文档中的描述执行OAuth 2.0 web流程。

相关文章: