How to get a new Google access token from a refresh token on Node.js


If you’re creating an app where you need to access Google services from a server, you’ll need to store the refresh token from the Google Oauth process. The access tokens periodically expire, so you need the refresh token to manually get a new access token when you need it.

So if you’re using the Passport auth middleware for Node.js you would have something like this to make sure you get the refresh token. Store this refresh token in a database.

https://gist.github.com/magician11/205764591ba3e7a8be6725d9ddc6f519#file-passport-js

Then when you need an access token to access gmail in this case, you could get a new access token like this

https://gist.github.com/magician11/205764591ba3e7a8be6725d9ddc6f519#file-google-oauth-js

— based on these Google Identity Platform docs


One response to “How to get a new Google access token from a refresh token on Node.js”

Leave a Reply

Your email address will not be published.