Google Plus API不会返回电子邮件地址

时间:2015-06-08 14:13:50

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

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

$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] => 

此处不会返回电子邮件地址。有谁知道这是什么问题?

2 个答案:

答案 0 :(得分:2)

我找到了通过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

然后在我的应用中,我要求

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" } ],
.... 

答案 1 :(得分:0)

一些事情:

  • 您使用的范围除非您请求email范围,否则您将不会收到电子邮件
  • 您使用的是哪种访问令牌?简单的API密钥无法检索电子邮件地址,您必须使用登录。

登录时,您将按照the Google Sign-in documentation中的说明执行OAuth 2.0网络流程。