googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.4k stars 1.92k forks source link

FetchError: Failed to retrieve verification certificates: request to https://www.googleapis.com/oauth2/v1/certs failed, reason: connect ETIMEDOUT #3028

Open hoerlanton opened 2 years ago

hoerlanton commented 2 years ago

Hi,

I have an issue, which reoccurs since a week or so in my node js backend. Back then, when the issue occurred the first time, I created a SO question: https://stackoverflow.com/questions/71460283/node-js-react-native-failed-to-retrieve-verification-certificates-google-auth-li

The issue now is the same.

What you're trying to do: I have a middleware where I am checking, if the user is allowed to use the backend routes. In this middleware (code below) I am checking if the refresh token, stored in the front-end of my react native app in a secure storage. With the refresh token I am retrieving a new access token in order to retrieve the userId and if the user is verified. If so I call the next() method and the user is allowed to use the routes.

My code:

` console.log('with refresh token a new access token is retrieved, in order to check if email_verified');

const OAuth2 = google.auth.OAuth2;

const oauth2Client = new OAuth2( config.CLIENT_ID_GOOGLE, // ClientID config.CLIENT_SECRET_GOOGLE, // Client Secret "https://www.sagly.at" // Redirect URL );

oauth2Client.setCredentials({ refresh_token: token, });

const res = new Promise((resolve, reject) => { oauth2Client.getAccessToken((err, token, res) => { if (err) { } else { resolve(res); } }); });

const idToken = await res;

const client = new OAuth2Client(config.CLIENT_ID_GOOGLE); result = await client.verifyIdToken({ idToken: idToken.data.id_token, audience: config.CLIENT_ID_GOOGLE, });

payload = result.getPayload(); req.userId = payload['sub'];

if (payload && payload.email_verified) { console.log('next called'); return 'next'; }`

Error message I am receiving: FetchError: Failed to retrieve verification certificates: request to https://www.googleapis.com/oauth2/v1/certs failed, reason: connect ETIMEDOUT

So I am just wondering what I am doing wrong here. Any help is appreciated.

Best, Anton

dudizimber commented 1 year ago

We see this happening a lot on our services as well. There is a fairly big percentage of requests failing because of that.

Failed to retrieve verification certificates: request to https://www.googleapis.com/oauth2/v1/certs failed, reason: socket hang up

MuzzamielAbrahams commented 1 year ago

I am seeing a similar issue as of recently: FetchError: Failed to retrieve verification certificates: request to https://www.googleapis.com/oauth2/v1/certs failed, reason: unable to get local issuer certificate