本文整理汇总了Golang中code/google/com/p/goauth2/oauth.Transport.Refresh方法的典型用法代码示例。如果您正苦于以下问题:Golang Transport.Refresh方法的具体用法?Golang Transport.Refresh怎么用?Golang Transport.Refresh使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类code/google/com/p/goauth2/oauth.Transport
的用法示例。
在下文中一共展示了Transport.Refresh方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: rtRoot
func rtRoot(w http.ResponseWriter, r *http.Request, args []string) error {
sess := getSession(r)
if sess == nil || sess.token == nil {
http.Redirect(w, r, "/oauth2/login", http.StatusFound)
return nil
}
t := oauth.Transport{
Config: oauthConfig,
Token: sess.token,
}
if er := t.Refresh(); er != nil {
return er
}
res, er := t.Client().Get("https://api.guildwars2.com/v2/account")
if er != nil {
return er
}
w.Header().Set("Content-Type", "application/json")
io.Copy(w, res.Body)
return nil
}
示例2: initTransport
func initTransport(transport *oauth.Transport) error {
// First: check the cache.
if token, err := transport.Config.TokenCache.Token(); err == nil {
// We have a token, refresh it. The lifetime is 1h, so we always
// refresh to ensure lengthy commands do not time out.
transport.Token = token
err := transport.Refresh()
if err == nil {
return nil
}
log.Infof("token refresh failed, requesting new one")
}
// Get a new token. Pops up a browser window (hopefully).
randState := fmt.Sprintf("st%d", time.Now().UnixNano())
authURL := transport.Config.AuthCodeURL(randState)
log.Infof("Opening auth URL in browser: %s", authURL)
log.Infof("If the URL doesn't open please open it manually and copy the code here.")
openURL(authURL)
code := getCodeFromStdin()
_, err := transport.Exchange(code)
if err != nil {
log.Infof("problem exchanging code: %v", err)
return err
}
return nil
}
示例3: hasValidToken
// Returns true if we have a valid cached token
func hasValidToken(cacheFile oauth.CacheFile, transport *oauth.Transport) bool {
// Check if we have a cached token
token, err := cacheFile.Token()
if err != nil {
return false
}
// Refresh token if its expired
if token.Expired() {
transport.Token = token
err = transport.Refresh()
if err != nil {
fmt.Println(err)
return false
}
}
return true
}