0

I am trying to just get an email address after authenticating with oAuth2. After I get a code when I validate with a google account, I go and get the user from Google Plus, like this:

let oAuth2Client = new auth.OAuth2(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GOOGLE_CLIENT_CALLBACK);
const tokens = await getToken(oAuth2Client, code);
oAuth2Client.setCredentials(tokens);
plus('v1').people.get({ userId: 'me', auth: oAuth2Client }, function(err, response) {
  .... This repsonse is the full user object from Google Plus
});

How do I just get the email address, or a list of email addresses? I don't want all the other Google Plus info. I have already set my scope to email, but I still get lots of other information. I am assuming that I need to make a request to something other than plus, but I can't find what to do in the documentation.

jhamm
  • 24,124
  • 39
  • 105
  • 179

1 Answers1

0

If you have set email scope (https://www.googleapis.com/auth/userinfo.email), you can use oauth2("v2").userinfo.v2.me.get :

var google = require('googleapis');

....

google.oauth2("v2").userinfo.v2.me.get({
    auth: oAuth2Client
}, function(e, profile) {
    if ("email" in profile) {
        console.log(profile.email);
    } else {
        console.log("scope email not set");
    }
});
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159