【发布时间】:2017-03-10 08:42:19
【问题描述】:
我能够通过 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。它可以工作一个小时(Expiry 的长度),但在那之后就停止工作了。
【问题讨论】:
-
获取代码时,需要在获取代码的URL中包含
access_type=offline。获取代码的URL是https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=###&redirect_uri=###I&scope=###&access_type=offline详细信息是developers.google.com/identity/protocols/OAuth2WebServer -
带有 oauth2.AccessTypeOffline 的 AuthCodeURL() 函数将“access_type=offline”添加到重定向 URL,所以这已经发生了。
标签: go google-oauth