How can I get email address from Google Plus API o

2019-04-07 16:54发布

问题:

I have got accesstoken using oauth2.0. I am able to get the person name, gender, etc but I am not able to get the email address of the user.

Could any one please paste some sample code or any suggestions on how to get the email address from the google plus API?

回答1:

You can retrieve a user's email address if they specifically authorize your application to see their email address.

Set your scopes to:

https://www.googleapis.com/auth/plus.login
https://www.googleapis.com/auth/userinfo.email

The JavaScript calls look like this:

gapi.client.load('oauth2', 'v2', function() {
  gapi.client.oauth2.userinfo.get().execute(function(resp) {
    // Shows user email
    console.log(resp.email);
  })
});

gapi.client.load('plus', 'v1', function() {
  gapi.client.plus.people.get( {'userId' : 'me'} ).execute(function(resp) {
    // Shows other profile information
    console.log(resp);
  })
});

More information https://developers.google.com/+.

Note that you do not need scopes for plus.me or userinfo.profile.



回答2:

Exposing E-mail addresses of people who have not set it to be visible to 'Public' would obviously be a privacy issue, so that's not possible.

Exposing E-mail addresses of people who have set their E-mail address visibility to 'Public' is possible, but not yet there. It is currently an open issue

Edit: The issue is resolved now, so you can follow the steps in the other answer to get it.