There are few examples of using https://github.com/golang/oauth2 but none of them covers usage of refresh tokens. I've tried few approaches, but i'm still unsatisfied with my results.
Is there any example code, or maybe you know some project at Github using oauth2 lib
to take as example?
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.