googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.37k stars 1.92k forks source link

Setting up hangs on authenticate() #3059

Open OakLoaf opened 2 years ago

OakLoaf commented 2 years ago

I am trying to setup the Google Docs API into a Node-JS file hosted on an ubuntu server. I've used the NodeJS Google Docs API Quickstart information and searched around for alternative answers but am unsure of what is the solution. The correct credentials.json is in place, however, when running: client = await authenticate({ scopes: SCOPES, keyfilePath: CREDENTIALS_PATH, }); It simply hangs for eternity, presumably this would be when a pop-up would appear on a Windows PC but this is not the case and no alternative seems to be obviously available.

bcoe commented 2 years ago

@CoolDCB I would expect your initialization logic to look more like this:

const {google} = require('googleapis');
const compute = google.compute('v1');

async function main () {
  const auth = new google.auth.GoogleAuth({
    // Scopes can be specified either as an array or as a single, space-delimited string.
    scopes: ['https://www.googleapis.com/auth/compute']
  });
  const authClient = await auth.getClient();

  // obtain the current project Id
  const project = await auth.getProjectId();

  // Fetch the list of GCE zones within a project.
  const res = await compute.zones.list({ project, auth: authClient });
  console.log(res.data);
}

main().catch(console.error);

Where you instantiate an auth client, and then pass it to your quest to the docs API.

Shouldn't need to explicitly call an authenticate method.

OakLoaf commented 2 years ago

Thank you for this code, I'll definitely have a look into this and get back to you as to whether it has worked or not!

Rhahkeem commented 9 months ago

So I just ran into this same issue and it looks like the answer is to set chrome as your default browser(?). I had mine set to Firefox on Ubuntu and it just woudn't do anything (ie hung indefinitely), but once i changed it to Chrome it popped up a new window (didn't bring it to the foreground) and I had to auth through there. @sqrrrl can you think of a reason that it only worked with Chrome as opposed to another browser?

mat250 commented 4 months ago

Same issue here where the URL is not open automatically by my system (an issue with the open lib ? Something broken on the OS, like no default browser ?).

A simple thing to do will be to display the target URL in the console to open it manually.

I've added a console.log just after this block on L144 on node_modules/@google-cloud/local-auth/build/src/index.js :

const authorizeUrl = client.generateAuthUrl({
    redirect_uri: redirectUri.toString(),
    access_type: 'offline',
    scope: scopes.join(' '),
});
console.log({ authorizeUrl })

Now I can click on the link from the terminal and open it in any browser.

Rhahkeem commented 4 months ago

I've found that this is due (in my case) to VSCode's integration with the open command. I was running it within VSCode's integrated terminal. When running it through the terminal itself, and through Webstorm I had no issues, regardless of default browser

mat250 commented 4 months ago

I've found that this is due (in my case) to VSCode's integration with the open command. I was running it within VSCode's integrated terminal. When running it through the terminal itself, and through Webstorm I had no issues, regardless of default browser

Interesting... It was also on VSCode terminal on my side

Rhahkeem commented 4 months ago

It was somehow missing some shared library object that prevented it from launching. no idea why. As soon as I ran it outside of the integrated terminal it worked with no issues