googleapis / google-auth-library-nodejs

🔑 Google Auth Library for Node.js
Apache License 2.0
1.73k stars 381 forks source link

Unable to obtain token #1761

Closed liuchengts closed 8 months ago

liuchengts commented 8 months ago

I encountered a problem with requesting the API and was unable to obtain the token. Operating system: windows11 x64 Environment: node:21.6.0 npm: 10.2.4 google-auth-library: 9.6.3

Steps to reproduce: Occurs when oauth2Client.getToken(code) is called:

unhandledRejection: GaxiosError: request to https://oauth2.googleapis.com/token failed, reason: read ECONNRESET
    at Gaxios._request (webpack-internal:///(rsc)/./node_modules/gaxios/build/src/gaxios.js:140:65)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async OAuth2Client.getTokenAsync (webpack-internal:///(rsc)/./node_modules/google-auth-library/build/src/auth/oauth2client.js:135:21) {
  config: {
    method: 'POST',
    url: 'https://oauth2.googleapis.com/token',
    data: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'User-Agent': 'google-api-nodejs-client/9.6.3',
      'x-goog-api-client': 'gl-node/21.6.0'
    },
    paramsSerializer: [Function: paramsSerializer],
    body: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
    validateStatus: [Function: validateStatus],
    responseType: 'unknown',
    errorRedactor: [Function: defaultErrorRedactor]
  },
  response: undefined,
  error: FetchError: request to https://oauth2.googleapis.com/token failed, reason: read ECONNRESET
      at ClientRequest.eval (webpack-internal:///(rsc)/./node_modules/node-fetch/lib/index.mjs:1333:20)
      at ClientRequest.emit (node:events:519:28)
      at TLSSocket.socketErrorListener (node:_http_client:492:9)
      at TLSSocket.emit (node:events:519:28)
      at emitErrorNT (node:internal/streams/destroy:169:8)
      at emitErrorCloseNT (node:internal/streams/destroy:128:3)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    type: 'system',
    errno: 'ECONNRESET',
    code: 'ECONNRESET'
  },
  code: 'ECONNRESET',
  [Symbol(gaxios-gaxios-error)]: '6.3.0'
}
null

Related code:

import {OAuth2Client} from "google-auth-library";
let oauth2Client: OAuth2Client | null
const scopes = [
    'https://www.googleapis.com/auth/userinfo.email',
    'https://www.googleapis.com/auth/userinfo.profile'
];
function getOauth2Client() {
    if (oauth2Client != null) return oauth2Client;
    const keys = require('../oauth2.keys.json');
    let redirectUri: string
    if (process.env.NODE_ENV == 'development') {
        redirectUri = keys.web.redirect_uris[1]
    } else {
        redirectUri = keys.web.redirect_uris[0]
    }
    oauth2Client = new OAuth2Client(
        keys.web.client_id,
        keys.web.client_secret,
        redirectUri
    );
    return oauth2Client
}

export function auth() {
    return getOauth2Client().generateAuthUrl({
        access_type: 'offline',
        scope: scopes
    });
}
// The authorization code has been obtained based on the URL. An error occurred when passing the code to obtain the token.
export function token(code: string) {
    getOauth2Client().getToken(code).then(res => {
        console.log("res:", res)
    })
}

please help me,thank you

liuchengts commented 8 months ago

I didn't change any code, everything works fine when used in the new environment, which is very strange. Operating system: macOS ventura 13.6.1 (22G313) node:v20.11.1 npm:10.2.4 google-auth-library:9.6.3

Compared to the problematic environment, there are the following differences:

  1. Operating system: windows11 x64 =》 macOS ventura 13.6.1 (22G313)
  2. node:21.6.0 =》v20.11.1
danielbankhead commented 8 months ago

@liuchengts In the Windows environment is it consistently an ECONNRESET error or sometimes? It may be related to this issue, as the endpoints require updating:

liuchengts commented 8 months ago

@liuchengts In the Windows environment is it consistently an ECONNRESET error or sometimes? It may be related to this issue, as the endpoints require updating:

In the windows environment I described at the beginning, it never succeeded. I thought it was a problem with npm install or a network problem. The actual situation is not

danielbankhead commented 8 months ago

That's strange, as it appears to work with Node 21 here on Windows (and in other environments):

Anything unique with the Windows setup networking? Proxy perhaps?

liuchengts commented 8 months ago

That's strange, as it appears to work with Node 21 here on Windows (and in other environments):

Anything unique with the Windows setup networking? Proxy perhaps?

There may be a Windows network problem, but I'm not sure because there is a network management and control software installed by the company on the Windows computer. I'm not sure if that's the influence. Currently this only happens when using google-auth-library.

Thank you for your help. I think this is an unsolvable problem because I can't determine what exactly causes getToken() ECONNRESET. So I think I can close this problem.

Thank you again for your help