3

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?

PsiKai
  • 1,803
  • 1
  • 5
  • 19

1 Answers1

6

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

  • 4
    hi, my refresh token keeps getting expired after a while is there any solution where it doesn't expire and i can send emails to all me clients of my web application ? – Rolstan D'souza May 21 '21 at 13:43
  • Your app must be verified for production setup. Otherwise refresh tokens will be expired quickly. Please refer https://stackoverflow.com/a/74816443/6674171 – Giri Dec 16 '22 at 15:36