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

Quickstart fails during authentication step #111

Closed
wescpy opened this issue Oct 4, 2021 · 3 comments · Fixed by #185
Closed

Quickstart fails during authentication step #111

wescpy opened this issue Oct 4, 2021 · 3 comments · Fixed by #185
Labels
type: question Request for information or clarification. Not an issue.

Comments

@wescpy
Copy link

wescpy commented Oct 4, 2021

Environment details

$ uname -a
Darwin wescpy-macbookpro1 20.6.0 Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64 x86_64
$ node -v
v16.9.1
$ npm -v
7.21.1
$ npm view @google-cloud/local-auth version
1.0.1

Steps to reproduce

  1. Copy quickstart sample into local dir as quickstart.js:
const {authenticate} = require('@google-cloud/local-auth');

async function quickstart() {
  const localAuth = await authenticate({
    scopes: ['https://www.googleapis.com/auth/blogger'],
    keyfilePath: '/path/to/keys.json',
  });
  console.log('Tokens:', localAuth.credentials);
}
quickstart();

2a. Run it (locally):

$ node quickstart.js
node:internal/modules/cjs/loader:933
  const err = new Error(message);
              ^

Error: Cannot find module '/path/to/keys.json'
Require stack:
- /Users/wescpy/node_modules/@google-cloud/local-auth/build/src/index.js
- /Users/wescpy/maestro/backup/google-apis/youtube/quickstart.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at authenticate (/Users/wescpy/node_modules/@google-cloud/local-auth/build/src/index.js:45:21)
    at quickstart (/Users/wescpy/maestro/backup/google-apis/youtube/quickstart.js:4:27)
    at Object.<anonymous> (/Users/wescpy/maestro/backup/google-apis/youtube/quickstart.js:10:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/wescpy/node_modules/@google-cloud/local-auth/build/src/index.js',
    '/Users/wescpy/maestro/backup/google-apis/youtube/quickstart.js'
  ]
}

2b. Run it in the cloud (Cloud Shell)... pretty much the same error:

wescpy@cloudshell:~/cloudshell_open/nodejs-local-auth/samples$ uname -a
Linux cs-807601953565-default-boost-sjpfp 5.4.144+ #1 SMP Sat Sep 25 09:56:01 PDT 2021 x86_64 GNU/Linux
wescpy@cloudshell:~/cloudshell_open/nodejs-local-auth/samples$ node -v
v12.14.1
wescpy@cloudshell:~/cloudshell_open/nodejs-local-auth/samples$ npm -v
7.24.1
wescpy@cloudshell:~/cloudshell_open/nodejs-local-auth/samples$ npm view @google-cloud/local-auth version
1.0.1
wescpy@cloudshell:~/cloudshell_open/nodejs-local-auth/samples$ node quickstart.js
(node:2267) UnhandledPromiseRejectionWarning: Error: Cannot find module '/path/to/keys.json'
Require stack:
- /home/wescpy/cloudshell_open/nodejs-local-auth/samples/node_modules/@google-cloud/local-auth/build/src/index.js
- /home/wescpy/cloudshell_open/nodejs-local-auth/samples/quickstart.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
    at Function.Module._load (internal/modules/cjs/loader.js:686:27)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at authenticate (/home/wescpy/cloudshell_open/nodejs-local-auth/samples/node_modules/@google-cloud/local-auth/build/src/index.js:45:21)
    at quickstart (/home/wescpy/cloudshell_open/nodejs-local-auth/samples/quickstart.js:19:27)
    at Object.<anonymous> (/home/wescpy/cloudshell_open/nodejs-local-auth/samples/quickstart.js:25:1)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
(node:2267) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:2267) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Obviously that filepath isn't real, but changing it to client_secret.json, which is a real file, still fails w/the same error.

  1. Just for the heck of it, I added use of the path module, to provide a full path:
const path = require('path');
const {authenticate} = require('@google-cloud/local-auth');

async function quickstart() {
  const localAuth = await authenticate({
    scopes: ['https://www.googleapis.com/auth/blogger'],
    keyfilePath: path.join(__dirname, 'client_secret.json')
  });
  console.log('Tokens:', localAuth.credentials);
}
quickstart();

It still fails, but with a different error now:

$ node quickstart.js
node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:371:5)
    at onParseError (node:internal/url:552:9)
    at new URL (node:internal/url:628:5)
    at authenticate (/Users/wescpy/node_modules/@google-cloud/local-auth/build/src/index.js:51:19)
    at quickstart (/Users/wescpy/maestro/backup/google-apis/youtube/quickstart.js:5:27)
    at Object.<anonymous> (/Users/wescpy/maestro/backup/google-apis/youtube/quickstart.js:11:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12) {
  input: 'oob',
  code: 'ERR_INVALID_URL'
}

Any help explaining/solving both of these issues will help... thanks!

@chingor13 chingor13 added the type: question Request for information or clarification. Not an issue. label Oct 5, 2021
@chingor13
Copy link

path/to/key.json in that sample needs to be replaced with the path to a credential json file (like an exported service account key, or user credentials).

An oauth client_secret.json is probably not a credential file (it's likely an OAuth app client secret file).

You might also want to look at https://github.com/googleapis/google-auth-library-nodejs intead of this repository

@wescpy
Copy link
Author

wescpy commented Oct 7, 2021

Thanks Jeff. So what you're saying is that this library is only for service account public/private keypair credentials JSON files, not OAuth client ID/secret JSON files? I thought that was well and swapped in the former, but this library still crashes because it's looking for redirect_uris which aren't in SA keypair creds files. Is it possible it has to be a web server OAuth client ID? (I was trying it for an installed app rather than for web apps.) Why does this library exist alongside the one you linked to? What's the difference b/w them, e.g., their differing use cases? And should this one be archived?

@willjk
Copy link

willjk commented Sep 29, 2022

@wescpy I had to change out the credentials path from

const CREDENTIALS_PATH = path.join(process.cwd(), 'credentials.json');

to

const CREDENTIALS_PATH = path.resolve(__dirname, "credentials.json");

You also might need to do the same thing with TOKEN_PATH. very annoying it doesn't work like the example

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

Successfully merging a pull request may close this issue.

3 participants