【问题标题】:Golang Google Drive Oauth2 Not Returning Refresh TokenGolang Google Drive Oauth2不返回刷新令牌
【发布时间】: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


【解决方案1】:

问题不在于代码,如果您以前从未经历过授权过程,代码就可以工作。如果您再次执行授权过程,则会出现此问题。不会向您显示所要求的权限,也不会发送刷新令牌。您必须强制再次显示权限对话框。为此,请将approval_prompt=force 添加到重定向 URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-03
    • 2023-01-02
    • 2021-08-27
    • 2022-06-19
    • 2015-04-09
    • 1970-01-01
    • 2021-07-05
    • 2013-08-30
    相关资源
    最近更新 更多