googleapis / google-auth-library-nodejs

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

Facing timeout issue for https://www.googleapis.com/oauth2/v4/token while generating oauth access token using google-auth-library #1786

Closed LMOG10 closed 5 months ago

LMOG10 commented 5 months ago

I have created an nodejs api to generate oauth2 accessToken using google-auth-library for FCM HTTP V1 API.

This api generates an oauth2 accessToken which will then be used by my react app to make a request to FCM HTTP V1 API.

The api code works fine on my local machine gives the excepted output. But when this code is deployed on server I am facing timeout issue for https://www.googleapis.com/oauth2/v4/token this endpoint.

Code :- module.exports.generateToken = async function (transactionId) { try { const SCOPES = "https://www.googleapis.com/auth/firebase.messaging"; const currentTimeZone = new Date(Date.now()); currentTimeZone.setHours( currentTimeZone.getHours() + config.AccessTokenExpiryTime ); const auth = new GoogleAuth({ credentials: { client_email: config.client_email, private_key: config.private_key, }, scopes: SCOPES, });

const client = await auth.getClient();
const accessToken = await client.getAccessToken();
globalaccessToken = accessToken.token;
tokenExpiry = currentTimeZone;

loggerDetail.debug(transactionId + " AccessToken Retrieved Successfully ");

return {
  accessTokenValue: globalaccessToken,
  tokenExpiration: tokenExpiry,
};

}catch (error) { loggerDetail.debug( transactionId + " Failed to retrieve access token: " + error.message ); } };

####################### Error while token generation: GaxiosError: request to https://www.googleapis.com/oauth2/v4/token failed, reason: connect ETIMEDOUT 142.250.192.170:443 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.665 +05:30: at Gaxios._request (/opt/MyTest/MyTest-ui/apis/MyTest-notification-get-accessToken/node_modules/gaxios/build/src/gaxios.js:148:19) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.665 +05:30: at processTicksAndRejections (node:internal/process/task_queues:96:5) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.665 +05:30: at async GoogleToken.requestToken (/opt/MyTest/MyTest-ui/apis/MyTest-notification-get-accessToken/node_modules/gtoken/build/src/index.js:230:23) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.665 +05:30: at async GoogleToken.getTokenAsync (/opt/MyTest/MyTest-ui/apis/MyTest-notification-get-accessToken/node_modules/gtoken/build/src/index.js:137:20) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.665 +05:30: at async JWT.refreshTokenNoCache (/opt/MyTest/MyTest-ui/apis/MyTest-notification-get-accessToken/node_modules/google-auth-library/build/src/auth/jwtclient.js:165:23) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.665 +05:30: at async JWT.refreshAccessTokenAsync (/opt/MyTest/MyTest-ui/apis/MyTest-notification-get-accessToken/node_modules/google-auth-library/build/src/auth/oauth2client.js:209:19) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.665 +05:30: at async JWT.getAccessTokenAsync (/opt/MyTest/MyTest-ui/apis/MyTest-notification-get-accessToken/node_modules/google-auth-library/build/src/auth/oauth2client.js:238:23) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.665 +05:30: at async Object.module.exports.generateToken (/opt/MyTest/MyTest-ui/apis/MyTest-notification-get-accessToken/service/notificationGetAccessTokenService.js:210:25) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.665 +05:30: at async Object.module.exports.invokeGetAccessToken (/opt/MyTest/MyTest-ui/apis/MyTest-notification-get-accessToken/service/notificationGetAccessTokenService.js:49:14) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.665 +05:30: at async notificationGetAccessToken (/opt/MyTest/MyTest-ui/apis/MyTest-notification-get-accessToken/controllers/notificationGetAccessTokenController.js:54:53) { 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.665 +05:30: config: { 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.665 +05:30: method: 'POST', 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: url: 'https://www.googleapis.com/oauth2/v4/token', 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: data: { 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: grant_type: '< - See errorRedactor option in gaxios for configuration>.', 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: assertion: '< - See errorRedactor option in gaxios for configuration>.' 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: }, 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: headers: { 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: 'Content-Type': 'application/x-www-form-urlencoded', 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: 'User-Agent': 'google-api-nodejs-client/9.2.0', 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: 'x-goog-api-client': 'gl-node/16.15.0', 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: Accept: 'application/json' 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: }, 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: responseType: 'json', 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: paramsSerializer: [Function: paramsSerializer], 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: body: '< - See errorRedactor option in gaxios for configuration>.', 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: validateStatus: [Function: validateStatus], 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: errorRedactor: [Function: defaultErrorRedactor] 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: }, 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: response: undefined, 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: error: FetchError: request to https://www.googleapis.com/oauth2/v4/token failed, reason: connect ETIMEDOUT 142.250.192.170:443 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.666 +05:30: at ClientRequest. (/opt/MyTest/MyTest-ui/apis/MyTest-notification-get-accessToken/node_modules/node-fetch/lib/index.js:1501:11) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.667 +05:30: at ClientRequest.emit (node:events:527:28) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.667 +05:30: at TLSSocket.socketErrorListener (node:_http_client:454:9) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.667 +05:30: at TLSSocket.emit (node:events:527:28) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.667 +05:30: at emitErrorNT (node:internal/streams/destroy:157:8) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.667 +05:30: at emitErrorCloseNT (node:internal/streams/destroy:122:3) 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.667 +05:30: at processTicksAndRejections (node:internal/process/task_queues:83:21) { 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.667 +05:30: type: 'system', 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.667 +05:30: errno: 'ETIMEDOUT', 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.667 +05:30: code: 'ETIMEDOUT' 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.667 +05:30: }, 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.667 +05:30: code: 'ETIMEDOUT' 4|MyTest UI Notification Get AccessToken | 20240313 16:46:06.667 +05:30: }

Node version on server :- v16.0.0 Node version on my local machine :- v16.20.0 library version :- ^9.6.3

Tried with library version : ^9.2 was facing same issue then migrated to 9.6.3

danielbankhead commented 5 months ago

This is currently tracked via:

I should have some updates shortly to resolve this issue.

LMOG10 commented 5 months ago

Does it means my issue has been resolved ?

LMOG10 commented 5 months ago

Can you provide node js implementation of google-auth-library for oauth 2.0 access token generation ?

danielbankhead commented 5 months ago

https://github.com/googleapis/google-auth-library-nodejs/blob/b2eae07d754ae60565217f58fd5f45bd82385c60/src/auth/oauth2client.ts#L634-L685

Daudxu commented 5 months ago

error: FetchError: request to https://www.googleapis.com/oauth2/v1/certs failed, reason: connect ETIMEDOUT 142.251.43.10:443

Daudxu commented 5 months ago

error: FetchError: request to https://oauth2.googleapis.com/token failed, reason: connect ETIMEDOUT 142.251.43.10:443

LMOG10 commented 5 months ago

I am using service-account.json credentials to generate a client and use that client to generate access token.

Provide node js implementation for above scenario where instead of client id & secret we are using service-account.json credentials

zhanzengyu commented 3 months ago

@danielbankhead any updates?

danielbankhead commented 2 months ago

This particular issue should be resolved as of 9.8.0+ with the following PR:

Is anyone experiencing this issue in the latest release?