I have a RoR app where I am authenticating against Google using omniauth and google_oauth2 where I am requesting offline access.
How do I use my refresh token to request a current access token? Also, how can I refresh my access token when it no longer works? I don’t want to have any user interface in this situation, assuming of course that the authorization hasn’t been taken away.
I don’t see anything in
google_oauth2that handles fetching a newaccess_tokenwith a refresh token, so it looks like you’ll need to make the exchange directly.Google’s official OAuth 2.0 documentation explains how to do this at a low level. Within your server-side code, use your favorite HTTP client to construct a request that looks like this:
where
CLIENT_IDandCLIENT_SECRETare the same ones you used for the original authentication andREFRESH_TOKENis the refresh token from the original authentication flow. If the exchange is successful, you’ll receive a fresh access token in a response that looks something like this:You can follow this process to grab a new access token whenever you need one. You can either use the
expires_invalue to estimate when you will need a new one, or attempt a refresh whenever your API request responds with a 401 HTTP status.