0
我可以通過oauth過程獲取我可以成功用於與GDrive進行交互的令牌。令牌具有AccessToken但不具有RefreshToken。我如何獲得RefreshToken?Golang Google Drive Oauth2未返回刷新令牌
這是在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)
這裏的時候/ gdrivecb被稱爲時調用的相關代碼(的OAuthConfig是和以前一樣,代碼是code
URL參數:
token, err = oauthconfig.Exchange(nil, code)
該令牌包含一個AccessToken但不包含RefreshToken,並且它可以工作一個小時(Expiry的長度),但之後會停止工作。
當檢索代碼時,需要在URL中包含「access_type = offline」來檢索代碼。用於檢索代碼的網址是「https://accounts.google.com/o/oauth2/v2/auth?response_type = code&client_id = ###&redirect_uri = ###我&scope = ###&access_type = offline''詳細信息是https://developers.google.com/identity/protocols/OAuth2WebServer – Tanaike
帶有oauth2.AccessTypeOffline的AuthCodeURL()函數將「access_type = offline」添加到重定向URL,因此已經發生。 –
對不起,我忍不住。 – Tanaike