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

Unable to obtain token #1761

Closed
liuchengts opened this issue Feb 27, 2024 · 5 comments
Closed

Unable to obtain token #1761

liuchengts opened this issue Feb 27, 2024 · 5 comments
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@liuchengts
Copy link

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 liuchengts added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Feb 27, 2024
@liuchengts
Copy link
Author

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
Copy link
Contributor

@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
Copy link
Author

@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
Copy link
Contributor

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants