本文只记录刷新oauth2 refresh token 的代码,oauth2不了解请自行百度
// clientId、clientSecret、oauthKey需要改成自己的
// golang.org/x/oauth2
func RefreshToken() (*oauth2.Token, error) {
clientId = ""
clientSecret = ""
oauthKey = ""
conf := oauth2.Config{
ClientID: clientId,
ClientSecret: clientSecret,
Endpoint: google.Endpoint,
RedirectURL: https://accounts.google.com/o/oauth2/token?,
}
refreshToken := &oauth2.Token{
AccessToken: "",
RefreshToken: oauthKey,
Expiry: time.Now().UTC(),
}
token, err := conf.TokenSource(context.TODO(), refreshToken).Token()
if err != nil {
return nil, err
}
return token, nil
}