duanfengwang9157 2018-10-15 22:16
浏览 60
已采纳

当访问令牌在GO中过期时,如何使用Google刷新令牌?

I'm new to use Google services and I'm currently trying to read emails from gmail API. However, I'm stuck in the scenario when the access token is expired.

I have the refresh token from the first authentication, however, some threads say that the access token is refreshed automatically when it expires. But mine didn't. I don't know where I'm wrong. Here's my code currently.

// get saved token from database / anywhere else
func getCachedToken() *oauth2.Token {
    token := new(oauth2.Token)
    token.AccessToken = "xxxxxxxx"
    token.RefreshToken = "xxxxxx"
    token.TokenType = "Bearer"
    return token
}

func refreshToken(config *oauth2.Config, token *oauth2.Token) *http.Client {
    return config.Client(context.Background(), token)
}

// Retrieve a token, saves the token, then returns the generated client.
func getClient(config *oauth2.Config) *http.Client {
    tok := getTokenFromWeb(config)
    fmt.Println(tok.RefreshToken)
    fmt.Println(tok.AccessToken)
    fmt.Println(tok.Expiry)
    fmt.Println(tok.TokenType)
    return config.Client(context.Background(), tok)
}

func main() (string, error) {
    b, err := ioutil.ReadFile("credentials.json")
    if err != nil {
        log.Fatalf("Unable to read client secret file: %v", err)
    }

    // If modifying these scopes, delete your previously saved token.json.
    config, err := google.ConfigFromJSON(b, gmail.GmailReadonlyScope, gmail.GmailSendScope, gauth.UserinfoEmailScope, gauth.UserinfoProfileScope)
    if err != nil {
        log.Fatalf("Unable to parse client secret file to config: %v", err)
    }
    token := getCachedToken()
    accessToken := token.AccessToken
    client := refreshToken(config, token)
    if err != nil {
        log.Fatalf("Unable to retrieve oauth client: %v", err)
    }
    if accessToken != token.AccessToken {
        fmt.Println(token.RefreshToken)
        fmt.Println(token.AccessToken)
        fmt.Println(token.Expiry)
        fmt.Println(token.TokenType)
    } //THE TOKEN IS NOT CHANGED HERE WHEN THE ACCESS TOKEN EXPIRED
    srv, err := gmail.New(client)
    if err != nil {
        log.Fatalf("Unable to retrieve Gmail client: %v", err)
    }

    user := "me"

    profile, err := srv.Users.GetProfile(user).Do()
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(profile.MessagesTotal)

    return "test", nil
}
  • 写回答

1条回答 默认 最新

  • douxigai8757 2018-10-16 03:26
    关注

    oauth2.Config has a function which can automatically refreshes the token.

    updatedToken, err := config.TokenSource(context.TODO(), token).Token()

    pass your old token there and the updatedToken will reflect the updated access token and expiry

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题