node.jsreactjsgoogle-apigoogle-api-nodejs-clientgoogle-auth-library

How to get email id as a part of google oauth 2.0 googl drive sign in


I am creating a website where in one page Google drive sign in is required. I am using Google's OAuth 2.0. I have used the below scopes

https://www.googleapis.com/auth/drive.metadata.readonly
 
https://www.googleapis.com/auth/drive

When I tried printing the info, email is showing as null. I don't want the email to be null. It needs to have the signed in Google drive email How do I get it ?

I tried using

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

But even then the email is not shown

const {google} = require('googleapis');

const oauth2Client = new google.auth.OAuth2(
  CLIENT_ID,
  CLIENT_SECRET,
  REDIRECT_URL
);

const scopes = [
  'https://www.googleapis.com/auth/drive.metadata.readonly',
  'https://www.googleapis.com/auth/drive',
  'https://www.googleapis.com/auth/userinfo.email'
];

const authorizationUrl = oauth2Client.generateAuthUrl({
  access_type: 'offline',
  scope: scopes,
});

Solution

  • I was able to arrive at a solution by making an axios GET call to https://www.googleapis.com/oauth2/v3/userinfo and passing the access token as bearer token