You need not bother about refreshing tokens until the time you are storing the Expiry
parameter. After getting the 'Token' object, store the following in your database:
token.AccessToken
, token.RefreshToken
, token.TokenType
and token.Expiry
while fetching, construct the token object again using the above parameters:
token := new(oauth2.Token)
token.AccessToken = {{ From DataBase }}
token.RefreshToken = {{ From DataBase }}
token.Expiry = {{ From DataBase }}
token.TokenType = {{ From DataBase }}
and then get your http client:
config.Client(ctx, token)
this will handle refreshing the token. Excerpt (more info: Golang oauth2 client):
Client returns an HTTP client using the provided token. The token will auto-refresh as necessary.
Only downside is, the refreshed access token is not returned. But it works! Google has no restrictions on how many times the refresh token is used.