本文整理汇总了Golang中golang.org/x/oauth2.Config.Client方法的典型用法代码示例。如果您正苦于以下问题:Golang Config.Client方法的具体用法?Golang Config.Client怎么用?Golang Config.Client使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/oauth2.Config
的用法示例。
在下文中一共展示了Config.Client方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: facebookHandler
// facebookHandler is a ContextHandler that gets the OAuth2 Token from the ctx
// to get the corresponding Facebook User. If successful, the user is added to
// the ctx and the success handler is called. Otherwise, the failure handler
// is called.
func facebookHandler(config *oauth2.Config, success, failure ctxh.ContextHandler) ctxh.ContextHandler {
if failure == nil {
failure = gologin.DefaultFailureHandler
}
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
token, err := oauth2Login.TokenFromContext(ctx)
if err != nil {
ctx = gologin.WithError(ctx, err)
failure.ServeHTTP(ctx, w, req)
return
}
httpClient := config.Client(ctx, token)
facebookService := newClient(httpClient)
user, resp, err := facebookService.Me()
err = validateResponse(user, resp, err)
if err != nil {
ctx = gologin.WithError(ctx, err)
failure.ServeHTTP(ctx, w, req)
return
}
ctx = WithUser(ctx, user)
success.ServeHTTP(ctx, w, req)
}
return ctxh.ContextHandlerFunc(fn)
}
示例2: _google
func (p *Engine) _google(code string) (*User, error) {
var cfg oauth2.Config
if err := p.Dao.Get("google.oauth", &cfg); err != nil {
return nil, err
}
tok, err := cfg.Exchange(oauth2.NoContext, code)
if err != nil {
return nil, err
}
cli := cfg.Client(oauth2.NoContext, tok)
res, err := cli.Get("https://www.googleapis.com/oauth2/v1/userinfo?alt=json")
if err != nil {
return nil, err
}
defer res.Body.Close()
var gu GoogleUser
dec := json.NewDecoder(res.Body)
if err := dec.Decode(&gu); err != nil {
return nil, err
}
return p.Dao.AddUser(gu.ID, "google", gu.Email, gu.Name, gu.Link, gu.Picture)
}
示例3: handleCallback
func handleCallback(w http.ResponseWriter, r *http.Request, config *oauth2.Config, profilesURL, code string) {
token, err := config.Exchange(oauth2.NoContext, code)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
client := config.Client(oauth2.NoContext, token)
response, err := client.Get(profilesURL)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer response.Body.Close()
rawBody, err := ioutil.ReadAll(response.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
s, _ := session.Manager.SessionStart(w, r)
defer s.SessionRelease(w)
s.Set("id_token", token.Extra("id_token"))
s.Set("access_token", token.AccessToken)
s.Set("profile", string(rawBody))
http.Redirect(w, r, "/user", http.StatusMovedPermanently)
}
示例4: Client
func Client(ctx context.Context, config *oauth2.Config, options ...Option) (*http.Client, *oauth2.Token, error) {
o := processOpts(options)
if o.filecache != nil {
tok, err := o.filecache.Token()
if err == nil {
o.tok = tok
}
}
if o.tok != nil {
tok, err := config.TokenSource(ctx, o.tok).Token()
if err == nil {
return config.Client(ctx, tok), tok, nil
}
}
tok, err := ExecuteFlow(ctx, config, options...)
if err != nil {
return nil, nil, err
}
if o.filecache != nil {
_ = o.filecache.Write(tok)
}
return config.Client(ctx, tok), tok, nil
}
示例5: googleHandler
// googleHandler is a ContextHandler that gets the OAuth2 Token from the ctx
// to get the corresponding Google Userinfoplus. If successful, the user info
// is added to the ctx and the success handler is called. Otherwise, the
// failure handler is called.
func googleHandler(config *oauth2.Config, success, failure ctxh.ContextHandler) ctxh.ContextHandler {
if failure == nil {
failure = gologin.DefaultFailureHandler
}
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
token, err := oauth2Login.TokenFromContext(ctx)
if err != nil {
ctx = gologin.WithError(ctx, err)
failure.ServeHTTP(ctx, w, req)
return
}
httpClient := config.Client(ctx, token)
googleService, err := google.New(httpClient)
if err != nil {
ctx = gologin.WithError(ctx, err)
failure.ServeHTTP(ctx, w, req)
return
}
userInfoPlus, err := googleService.Userinfo.Get().Do()
err = validateResponse(userInfoPlus, err)
if err != nil {
ctx = gologin.WithError(ctx, err)
failure.ServeHTTP(ctx, w, req)
return
}
ctx = WithUser(ctx, userInfoPlus)
success.ServeHTTP(ctx, w, req)
}
return ctxh.ContextHandlerFunc(fn)
}
示例6: getClient
func getClient(config *oauth2.Config, code string) (*drive.Service, error) {
if config == nil {
return nil, errors.New("Google Drive is not configured")
}
token := &oauth2.Token{AccessToken: code}
return drive.New(config.Client(context.Background(), token))
}
示例7: 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
}
示例8: getSheetClient
// getClient uses a Context and Config to retrieve a Token
// then generate a Client. It returns the generated Client.
func getSheetClient(ctx context.Context, authConfig *oauth2.Config) *http.Client {
tok, err := tokenFromFile(config.SheetCredentials)
if err != nil {
tok = getTokenFromWeb(authConfig)
saveToken(config.SheetCredentials, tok)
}
return authConfig.Client(ctx, tok)
}
示例9: createClient
func createClient(cfg *oauth2.Config) (*http.Client, error) {
tok, err := getToken(cfg)
if err != nil {
return nil, err
}
return cfg.Client(context.Background(), tok), nil
}
示例10: NewClientTokenTLS
// NewClientTokenTLS returns a client at the specified url that
// authenticates all outbound requests with the given token and
// tls.Config if provided.
func NewClientTokenTLS(uri, token string, c *tls.Config) Client {
config := new(oauth2.Config)
auther := config.Client(oauth2.NoContext, &oauth2.Token{AccessToken: token})
if c != nil {
auther.Transport.(*oauth2.Transport).Base = &http.Transport{TLSClientConfig: c}
}
return &client{auther, uri}
}
示例11: GetClient
// GetClient uses a Context and Config to retrieve a Token
// then generate a Client. It returns the generated Client.
func GetClient(ctx context.Context, config *oauth2.Config) *http.Client {
cacheFile := tokenCacheFile()
tok, err := tokenFromFile(cacheFile)
if err != nil {
tok = getTokenFromWeb(config)
saveToken(cacheFile, tok)
}
return config.Client(ctx, tok)
}
示例12: getClient
// getClient uses a Context and Config to retrieve a Token
// then generate a Client. It returns the generated Client.
func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
cacheFile := filepath.Join("./.credentials", url.QueryEscape("gmail_token.json"))
token, err := tokenFromFile(cacheFile)
if err != nil {
log.Fatalf("Unable to retrieve token from cached credential file. %v", err)
}
return config.Client(ctx, token)
}
示例13: ServeHTTP
// TODO: cache the twitter api call
// TODO: implement OPTIONS
func (l LinkerssHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
log.Printf("%+v\n", req)
// TODO: has to be a cleaner pattern than this to get an int from query params
numTweets := l.DefaultNumTweets
numTweetsStr := req.URL.Query().Get("numTweets")
if numTweetsStr != "" {
var err error
numTweets, err = strconv.Atoi(numTweetsStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("invalid numTweets %s", numTweetsStr)))
return
}
}
if numTweets > l.MaxNumTweets {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("invalid numTweets %d, must be less than %d",
numTweets, l.MaxNumTweets)))
return
}
// set up the twitter client
config := oauth2.Config{}
token := oauth2.Token{AccessToken: l.AccessToken}
httpClient := config.Client(oauth2.NoContext, &token)
client := twitter.NewClient(httpClient)
// fetch the timeline
screenName := req.URL.Query().Get("screenName")
userTimelineParams := &twitter.UserTimelineParams{
ScreenName: screenName, Count: numTweets}
tweets, _, err := client.Timelines.UserTimeline(userTimelineParams)
if err != nil {
log.Fatal("error getting user timeline: %v", err)
}
// filter down to just tweets with urls
urlTweets := make([]twitter.Tweet, 0)
for _, t := range tweets {
if t.Entities.Urls != nil {
urlTweets = append(urlTweets, t)
}
}
// turn them into feed entries
feed := tweetsToFeed(urlTweets, screenName, l.Pool)
// write back to client as rss
err = feed.WriteRss(w)
if err != nil {
log.Fatal("error outputting as rss: %v", err)
}
}
示例14: newOAuthClient
func newOAuthClient(ctx context.Context, config *oauth2.Config) *http.Client {
cacheFile := tokenCacheFile(config)
token, err := tokenFromFile(cacheFile)
if err != nil {
token = tokenFromWeb(ctx, config)
saveToken(cacheFile, token)
} else {
log.Printf("Using cached token %#v from %q", token, cacheFile)
}
return config.Client(ctx, token)
}
示例15: getClient
// getClient uses a Context and Config to retrieve a Token
// then generate a Client. It returns the generated Client.
func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
cacheFile, err := tokenCacheFile()
if err != nil {
log.Fatalf("Unable to get path to cached credential file. %v", err)
}
tok, err := tokenFromFile(cacheFile)
if err != nil {
tok = getTokenFromWeb(config)
saveToken(cacheFile, tok)
}
return config.Client(ctx, tok)
}