本文整理汇总了Golang中golang.org/x/oauth2.Token.Valid方法的典型用法代码示例。如果您正苦于以下问题:Golang Token.Valid方法的具体用法?Golang Token.Valid怎么用?Golang Token.Valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/oauth2.Token
的用法示例。
在下文中一共展示了Token.Valid方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TokenOAuthClient
// TokenOAuthClient returns an oauth2 client for a specific token
func (c *client) TokenOAuthClient(ctx context.Context, config *oauth2.Config, userToken *oauth2.Token) (client *http.Client, err error) {
if !userToken.Valid() { // if user token is expired
userToken = &oauth2.Token{RefreshToken: userToken.RefreshToken}
}
return config.Client(ctx, userToken), err
}
示例2: UserOAuthClient
// UserOAuthClient returns an oauth2 client for a specific user
func (c *client) UserOAuthClient(ctx context.Context, config *oauth2.Config, userID string) (client *http.Client, err error) {
var userToken *oauth2.Token
if userToken, err = c.GetCachedToken(userID); err != nil {
// if token for user is not cached then go through oauth2 flow
if userToken, err = c.newUserToken(ctx, config, userID); err != nil {
return
}
}
if !userToken.Valid() { // if user token is expired
userToken = &oauth2.Token{RefreshToken: userToken.RefreshToken}
}
return config.Client(ctx, userToken), err
}
示例3: cachedToken
// cachedToken returns the token saved in cacheFile. It specifically returns
// errTokenExpired if the token is expired.
func cachedToken(cacheFile string) (*oauth2.Token, error) {
tok := new(oauth2.Token)
tokenData, err := wkfs.ReadFile(cacheFile)
if err != nil {
return nil, err
}
if err = json.Unmarshal(tokenData, tok); err != nil {
return nil, err
}
if !tok.Valid() {
if tok != nil && time.Now().After(tok.Expiry) {
return nil, errExpiredToken
}
return nil, errors.New("invalid token")
}
return tok, nil
}
示例4: getTokenContainer
func getTokenContainer(ctx *gin.Context) (*TokenContainer, bool) {
var oauth_token *oauth2.Token
var tc *TokenContainer
var err error
if oauth_token, err = extractToken(ctx.Request); err != nil {
glog.Errorf("Can not extract oauth2.Token, caused by: %s", err)
return nil, false
}
if !oauth_token.Valid() {
glog.Infof("Invalid Token - nil or expired")
return nil, false
}
if tc, err = GetTokenContainer(oauth_token); err != nil {
glog.Errorf("Can not extract TokenContainer, caused by: %s", err)
return nil, false
}
return tc, true
}