1

I'm using the javascript api for Google Auth 2.0 . I'm running into the problem where the users email is not showing up, even though I request with https://www.googleapis.com/auth/userinfo.email .

My code looks like this:

gapi.auth.authorize({
                    client_id : 'xxxxxxxxxx.apps.googleusercontent.com',
                    scope : ['https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'],
                    immediate : false
                }, function(result) {

                    if (result != null) {
                        gapi.client.load('plus', 'v1', function() {
                            var request = gapi.client.plus.people.get({
                                'userId' : 'me'
                            });
                            request.execute(function(resp) {
                                console.log(resp);

                            });
                        });

                    }
                });

What am I missing to get the user's email?

Kara
  • 6,115
  • 16
  • 50
  • 57
Devin Dixon
  • 11,553
  • 24
  • 86
  • 167

3 Answers3

4

While the userinfo.email role gives you access to the information, the plus v1 client doesn't provide it. You will need to make an additional call to a different endpoint to get the info.

You will need the oauth2 v2 endpoint, which you can request with gapi.client.load('oauth2', 'v2', callback). The endpoint itself that you want is gapi.client.oauth2.userinfo.get(). This is untested, but the code might look something like:

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

See How can I get the user's email address in google apps? and Why can't I retrieve the users email from Google Plus API after getting permission for some related questions and https://developers.google.com/accounts/docs/OAuth2 for more details from the official doc.

Community
  • 1
  • 1
Prisoner
  • 49,922
  • 7
  • 53
  • 105
2

Here's how I did it:

function tryAuth() {
    var clientId = CLIENT_ID;

    var configString = {
        client_id: clientId,
        scope: SCOPE,
        immediate: 'false'
    };

    gapi.auth.authorize(configString, handleAuthResult);
}

Where SCOPE = 'https://www.googleapis.com/auth/fusiontables email';

Replace https://www.googleapis.com/auth/fusiontables scope with your scope but keep ' email' .

      function handleAuthResult(authResult) {

          if (authResult && !authResult.error) {
            var access_token = authResult.access_token;
             alert('Successfully logged in.' + access_token);

            tryGetEmail(access_token);

    }

And then

function tryGetEmail(access_token) {
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.open("GET", 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + access_token, false );

            xmlHttp.send( null );

        if(xmlHttp.status == 200) {
            var strJSON = xmlHttp.responseText;
            var objJSON = eval("(function(){return " + strJSON + ";})()");
            email = objJSON.email;
            alert('got email ' + email);
        }
}
user1679059
  • 41
  • 1
  • 6
0

The userinfo endpoint and oauth2 v2 are being deprecated. The older answers are for the old system. All the details for migration are here:

https://developers.google.com/+/api/auth-migration#email

In short: put 'email' instead of 'h ttps://www.googleapis.com/auth/userinfo.email' for your scope, and the G+ email will be included as the first entry of the 'emails' property in the 'person' object you're fetching. There's also apparently an option described in the link to pull it out of the ID token, referenced in the link above.

Full example