Google-api-nodejs-client: Login Required message when using the googleapis client, but not when trying it manually

Created on 2 Jan 2019  路  5Comments  路  Source: googleapis/google-api-nodejs-client

Environment details

  • OS: Ubuntu 16.04
  • Node.js version: 10.15
  • npm version: 6.4.1
  • googleapis version: 36.0.0

Steps to reproduce

I'm using the googleapis npm as a Google API client in NodeJS.

Background:
A part of the app that runs separately obtains a user's OAuth access_token and refresh_token and stores it for the rest of the app to use later.

The code that I'm having issues with does the following:

It creates an oauth2Client, based on the docs:

const oauth2Client = new google.auth.OAuth2(client_id,client_secret);
oauth2Client.setCredentials({
      refresh_token: content.refresh_token,
      access_token: content.access_token,
});

Then it sets the drive object, as per the docs:

const drive = google.drive({ version: 'v3', oauth2Client });

Then, if I make a call to ANY endpoint, I get a 401 Error "Login Required".

Example usage:

drive.about.get({fields:["user"]}, (err, resp)=>{
  if (err) { console.log('Error with about:', err); }
  else { console.log('Response for about:', resp); }
});

I verified that both the code that obtains the tokens and this code are using the same client_id and client_secret.

Here is the fun part. As part of the debugging process, I put in a manual request to the same endpoint: https://www.googleapis.com/drive/v3/about?fields=user in the code, using the access_token for the Authorization header, and it WORKED.

(async () => {
  const params = {
    headers: {
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '*',
      'Authorization': 'Bearer ' + content.access_token
    }
  };
  try {
    const response = await got('https://www.googleapis.com/drive/v3/about?fields=user', params);
    console.log('***** RESPONSE WORKED:', response.body);
  } catch (error) {
    console.log('***** RESPONSE FAILED:', error.response.body);
  }
})();

This code actually returns a valid response.

There seems to be some sort of bug with the googleapis library having to do with authentication being set/being sent to endpoints.

I've tried calling some of the other endpoints down the line in my app manually instead of with the googleapis client and they work (no Login Required error).

needs more info question

Most helpful comment

Greetings! The problem may be this line:

const drive = google.drive({ version: 'v3', oauth2Client });

That should be:

const drive = google.drive({ version: 'v3', auth: oauth2Client });

Hope this helps!

All 5 comments

Greetings! The problem may be this line:

const drive = google.drive({ version: 'v3', oauth2Client });

That should be:

const drive = google.drive({ version: 'v3', auth: oauth2Client });

Hope this helps!

Wow.... sigh. You are 100% correct!

May I buy you a drink? :)

Amazing

I've had the exact same odd issue following outdated documentation.

If anybody is up for it, you can PR the improper instance creation here https://github.com/gsuitedevs/node-samples/blob/master/classroom/quickstart/index.js#L92

It should use the auth: auth and not just auth as suggested in the fix in this thread

OK, I've reported it. Thanks!

Was this page helpful?
0 / 5 - 0 ratings