Syed Asad Abbas Zaidi
Syed Asad Abbas Zaidi

Reputation: 1066

Get Email from id_token Gmail OAuth

I have a JSON Object with following details:

{ access_token: 'access_token',
  token_type: 'Bearer',
  expires_in: 3599,
  id_token: 'longstring' 
}

Using NodeJs library 'googleapis' how can I get the email address for the above response. I do not want to use the URL: https://www.googleapis.com/oauth2/v1/userinfo?access_token=your_access_token

Upvotes: 2

Views: 512

Answers (1)

Tholle
Tholle

Reputation: 112787

Get an auth-object like in the introduction, and then you can use the getProfile method:

function getEmailAddress(auth, callback) {
  gmail.users.getProfile({
    auth: auth,
    userId: 'me'
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      callback(err, null);
    } else {
      callback(null, response.emailAddress)
    }
  });
}

Upvotes: 1

Related Questions