Golang Google Drive Oauth2没有返回刷新令牌

时间:2017-03-09 23:58:43

标签: go google-oauth

我能够通过oauth程序获得一个我能够成功用来与GDrive交互的令牌。令牌具有AccessToken但没有RefreshToken。我如何获得RefreshToken?

这是在网络服务中。这是启动oauth授权程序的代码:

// Set up a configuration.
oauthconfig := &oauth2.Config{
    ClientID:     XXX,
    ClientSecret: XXX,
    RedirectURL:  "https://MYDOMAIN/gdrivecb",
    Scopes:       []string{"https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/drive"},
    Endpoint: oauth2.Endpoint{
        AuthURL:  "https://accounts.google.com/o/oauth2/auth",
        TokenURL: "https://accounts.google.com/o/oauth2/token",
    },
}
url := oauthconfig.AuthCodeURL(MYSCOPEDATA, oauth2.AccessTypeOffline)
http.Redirect(w, r, url, http.StatusFound)

这是调用/ gdrivecb时调用的相关代码(oauthconfig与以前相同,代码是code URL参数:

token, err = oauthconfig.Exchange(nil, code)

该令牌包含AccessToken但不包含RefreshToken。它工作了一个小时(到期时间长度),但之后停止工作。

1 个答案:

答案 0 :(得分:0)

问题不在于代码,如果您之前从未完成授权过程,则代码可以正常运行。如果您再次通过授权过程,则会出现此问题。您未显示要求的权限,并且不会发送刷新令牌。您必须强制再次显示权限对话框。为此,请将approval_prompt=force添加到重定向网址。

相关问题