Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
morfie78 opened this issue Jan 2, 2019 · 8 comments
Assignees
Labels
needs more info This issue needs more information from the customer to proceed. type: question Request for information or clarification. Not an issue.

Comments

@morfie78
Copy link

morfie78 commented Jan 2, 2019

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

@JustinBeckwith
Copy link
Contributor

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!

@JustinBeckwith JustinBeckwith added needs more info This issue needs more information from the customer to proceed. type: question Request for information or clarification. Not an issue. labels Jan 2, 2019
@morfie78
Copy link
Author

morfie78 commented Jan 2, 2019

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

May I buy you a drink? :)

@morfie78 morfie78 closed this as completed Jan 2, 2019
@marciofirmino
Copy link

Amazing

@AlmogCohen
Copy link

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

@AlmogCohen
Copy link

OK, I've reported it. Thanks!

@geuis
Copy link

geuis commented Nov 23, 2021

Was just caught by this documentation issue myself here at the end of 2021.

@shikhers16
Copy link

finally!, had been trying to fix this issue for a week due to the wrong documentation.
Thanks!!

@aguyinmontreal
Copy link

chatGPT does this mistake

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more info This issue needs more information from the customer to proceed. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

7 participants