In my Node.js application I use googleapis with oauth2 authentication to send an email. Suddenly, I Found this error when I start the application.
(node:1333) UnhandledPromiseRejectionWarning: Error: invalid_grant
at Gaxios._request (/Users/danielefarina/Desktop/GIT/server-worldmarker/src/node_modules/gaxios/build/src/gaxios.js:85:23)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async OAuth2Client.refreshTokenNoCache (/Users/danielefarina/Desktop/GIT/server-worldmarker/src/node_modules/google-auth-library/build/src/auth/oauth2client.js:170:21)
at async OAuth2Client.refreshAccessTokenAsync (/Users/danielefarina/Desktop/GIT/server-worldmarker/src/node_modules/google-auth-library/build/src/auth/oauth2client.js:194:19)
at async OAuth2Client.getAccessTokenAsync (/Users/danielefarina/Desktop/GIT/server-worldmarker/src/node_modules/google-auth-library/build/src/auth/oauth2client.js:214:23)
(node:1333) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1333) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Have you any idea on how to resolve it?
I had the exact same issue and I was able to fix this by regenerating the refresh_token in the oauthplayground. Specifically I did step 4 from this guide in order to get a new refresh token, which I replaced in my app and with existing client_id & secret_id everything continue working as expected.
I'm no oauth expert so I won't be able to fully explain why this happened, but after doing a little research I found that refresh tokens do expire (I thought they didn't), and for some reason my refresh token, which was maybe one month old, suddenly stopped working.
This is a good article that explains a little bit about refresh tokens: https://medium.com/@bantic/more-oauth-2-0-surprises-the-refresh-token-1831d71f4af6.
And in here you can find official explanation on refresh token expire: https://developers.google.com/identity/protocols/oauth2#expiration
I hope this helps you solving your problems.
Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With