1

I want to do Gmail authentication with access token. I am passing values like ,

const options = {
    method: 'GET',
    uri: 'https://www.googleapis.com/auth/plus.me',
    qs: {
        access_token: req.body.gmailToken,
        fields: 'id,name,first_name,last_name,email,picture'
    }
};

But I am not able to get auth data from gmail.

Is there another way to get auth data from Gmail Only by passing accessToken?

3
  • Please include some more details. Where is that gmailToken coming from? Also why are you accessing Google Plus instead of gmail? Commented Oct 25, 2017 at 5:46
  • req.body.gmailToken from request. And I don't have any idea what to pass in uri. I have made same authentication like this for facebook. Commented Oct 25, 2017 at 5:48
  • still pretty unclear to me. Google has a very detailed documentation for this. Since "from request" could be almost anything, I won't bother asking for more details I'm afraid of getting another too vague answer... Commented Oct 25, 2017 at 6:02

3 Answers 3

2

uri: 'https://www.googleapis.com/auth/plus.me',

Is the end point for the Google+ api no the gmail api. Gmail endpoint looks like this

https://www.googleapis.com/gmail/v1/

The following tutorial mail help Node.js Quickstart - Gmail

Gmail Authentication

/**
 * Create an OAuth2 client with the given credentials, and then execute the
 * given callback function.
 *
 * @param {Object} credentials The authorization client credentials.
 * @param {function} callback The callback to call with the authorized client.
 */
function authorize(credentials, callback) {
  var clientSecret = credentials.installed.client_secret;
  var clientId = credentials.installed.client_id;
  var redirectUrl = credentials.installed.redirect_uris[0];
  var auth = new googleAuth();
  var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

  // Check if we have previously stored a token.
  fs.readFile(TOKEN_PATH, function(err, token) {
    if (err) {
      getNewToken(oauth2Client, callback);
    } else {
      oauth2Client.credentials = JSON.parse(token);
      callback(oauth2Client);
    }
  });
}
Sign up to request clarification or add additional context in comments.

1 Comment

well you might want to try with the correct url that was just an example there is a lot of urls actually one for each method you are trying to access you may want to consult the documentation developers.google.com/gmail/api/v1/reference Using a Google+ endpoint is not going to result in Gmail data. You need to find the correct gmail end point for the method you are trying to access.
1
https://www.googleapis.com/plus/v1/people/me?access_token=token

This works for me. From that can get user information.

Comments

0

Try With:

For gmail authentication

https://www.googleapis.com/auth/plus.me?token=access_token

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.