Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Golang Google Drive Oauth2未返回刷新令牌_Go_Google Oauth - Fatal编程技术网

Golang Google Drive Oauth2未返回刷新令牌

Golang Google Drive Oauth2未返回刷新令牌,go,google-oauth,Go,Google Oauth,我能够通过oauth过程获得一个令牌,我能够成功地使用它与GDrive进行交互。令牌具有AccessToken,但没有RefreshToken。我怎样才能得到一个令牌 这是在一个web服务中。以下是启动oauth授权过程的代码: // Set up a configuration. oauthconfig := &oauth2.Config{ ClientID: XXX, ClientSecret: XXX, RedirectURL: "https://

我能够通过oauth过程获得一个令牌,我能够成功地使用它与GDrive进行交互。令牌具有AccessToken,但没有RefreshToken。我怎样才能得到一个令牌

这是在一个web服务中。以下是启动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)
以下是调用/gdrivebc时调用的相关代码(oauthconfig与以前相同,代码是
code
URL参数:

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

该令牌包含AccessToken,但不包含RefreshToken,有效期为一小时(到期时间),但随后停止工作。

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

检索代码时,需要将
access\u type=offline
包含在检索代码的URL中。检索代码的URL为
https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=###&redirect_uri=####I&scope=######(访问类型=offline)
详细信息是带有oauth2的AuthCodeURL()函数。AccessTypeOffline将“访问类型=offline”添加到重定向URL,因此这已经发生了。