Skip to content
This repository was archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
Add TokenForConfigAndCredentials helper.
Browse files Browse the repository at this point in the history
For my gRPC stuff I don't want an http.Client (maybe with Go 1.6 and
http2 by default I can use it straight up, but not yet). I need a token
only, so I wrote this helper.
  • Loading branch information
dpetersen committed Feb 3, 2016
1 parent 591324c commit 9b3fbc5
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,28 @@ func PasswordAuthenticatedClientFromConfig(conf *oauth2.Config, username, passwo
ctx = context.Background()
}

token, err := conf.PasswordCredentialsToken(ctx, username, password)
token, err := TokenForConfigAndCredentials(conf, username, password, ctx)
if err != nil {
return nil, err
}

return conf.Client(ctx, token), nil
}

// TokenForConfigAndCredentials will return a token using the passed-in config
// and credentials, and an optional context. This is likely to be used if you
// need to obtain a token, but will be arranging your own transport. Otherwise,
// you probably want the http.Client that PasswordAuthenticatedClientFromConfig
// can give you.
func TokenForConfigAndCredentials(conf *oauth2.Config, username, password string, ctx context.Context) (*oauth2.Token, error) {
if ctx == nil {
ctx = context.Background()
}

token, err := conf.PasswordCredentialsToken(ctx, username, password)
if err != nil {
return nil, err
}

return token, nil
}

0 comments on commit 9b3fbc5

Please sign in to comment.