Google plus API not return the Email address

1k views Asked by At

I'm using the following code for get the users details via google plus login.

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

but it returns the following details only

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

the Email address is not returned here. anybody know what is the problem?

2

There are 2 answers

1
class On

A few things:

  • What scope are you using, unless you request the email scope, you will not receive email
  • What kind of access token are you using? A simple API key cannot retrieve an email address, you must use Sign-in.

For Sign-in, you will do the OAuth 2.0 web flow as described in the Google Sign-in documentation.

0
Veck Hsiao On

I found the solution to successfully fetch email address by Google+ API.

First, I used the following example to get the permission url for client:

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  

Note that the scope should be

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

instead of

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

And then in my app, I requested

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

Or just called API like this question mentioned above:

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

Finally, I got my email in response:

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