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

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

Closed
LMOG10 opened this issue Apr 4, 2024 · 8 comments
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

@LMOG10
Copy link

LMOG10 commented Apr 4, 2024

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

@LMOG10 LMOG10 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 Apr 4, 2024
@danielbankhead
Copy link
Member

This is currently tracked via:

I should have some updates shortly to resolve this issue.

@danielbankhead danielbankhead closed this as not planned Won't fix, can't repro, duplicate, stale Apr 8, 2024
@LMOG10
Copy link
Author

LMOG10 commented Apr 15, 2024

Does it means my issue has been resolved ?

@LMOG10
Copy link
Author

LMOG10 commented Apr 15, 2024

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

@danielbankhead
Copy link
Member

/**
* Gets the access token for the given code.
* @param code The authorization code.
* @param callback Optional callback fn.
*/
getToken(code: string): Promise<GetTokenResponse>;
getToken(options: GetTokenOptions): Promise<GetTokenResponse>;
getToken(code: string, callback: GetTokenCallback): void;
getToken(options: GetTokenOptions, callback: GetTokenCallback): void;
getToken(
codeOrOptions: string | GetTokenOptions,
callback?: GetTokenCallback
): Promise<GetTokenResponse> | void {
const options =
typeof codeOrOptions === 'string' ? {code: codeOrOptions} : codeOrOptions;
if (callback) {
this.getTokenAsync(options).then(
r => callback(null, r.tokens, r.res),
e => callback(e, null, e.response)
);
} else {
return this.getTokenAsync(options);
}
}
private async getTokenAsync(
options: GetTokenOptions
): Promise<GetTokenResponse> {
const url = this.endpoints.oauth2TokenUrl.toString();
const values = {
code: options.code,
client_id: options.client_id || this._clientId,
client_secret: this._clientSecret,
redirect_uri: options.redirect_uri || this.redirectUri,
grant_type: 'authorization_code',
code_verifier: options.codeVerifier,
};
const res = await this.transporter.request<CredentialRequest>({
...OAuth2Client.RETRY_CONFIG,
method: 'POST',
url,
data: querystring.stringify(values),
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
});
const tokens = res.data as Credentials;
if (res.data && res.data.expires_in) {
tokens.expiry_date = new Date().getTime() + res.data.expires_in * 1000;
delete (tokens as CredentialRequest).expires_in;
}
this.emit('tokens', tokens);
return {tokens, res};
}

@Daudxu
Copy link

Daudxu commented Apr 24, 2024

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

@Daudxu
Copy link

Daudxu commented Apr 24, 2024

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

@LMOG10
Copy link
Author

LMOG10 commented Apr 24, 2024

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

@danielbankhead any updates?

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

4 participants