Skip to content

Login Required message when using the googleapis client, but not when trying it manuallyΒ #1523

@morfie78

Description

@morfie78

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).

Metadata

Metadata

Assignees

Labels

needs more infoThis issue needs more information from the customer to proceed.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions