From efb10a30610e617dbb17fc243f4cc61a8cfa2903 Mon Sep 17 00:00:00 2001 From: Jaana Burcu Dogan Date: Thu, 2 Mar 2017 12:04:53 -0800 Subject: [PATCH] oauth2: add example how to use a custom HTTP client Change-Id: Iffff423c167610c80e8dd1c51945c32b781e8653 Reviewed-on: https://go-review.googlesource.com/37695 Reviewed-by: Chris Broadfoot --- example_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/example_test.go b/example_test.go index d861fe7..378c70d 100644 --- a/example_test.go +++ b/example_test.go @@ -8,6 +8,8 @@ import ( "context" "fmt" "log" + "net/http" + "time" "golang.org/x/oauth2" ) @@ -45,3 +47,25 @@ func ExampleConfig() { client := conf.Client(ctx, tok) client.Get("...") } + +func ExampleHTTPClient() { + hc := &http.Client{Timeout: 2 * time.Second} + ctx := context.WithValue(context.Background(), oauth2.HTTPClient, hc) + + conf := &oauth2.Config{ + ClientID: "YOUR_CLIENT_ID", + ClientSecret: "YOUR_CLIENT_SECRET", + Scopes: []string{"SCOPE1", "SCOPE2"}, + Endpoint: oauth2.Endpoint{ + AuthURL: "https://provider.com/o/oauth2/auth", + TokenURL: "https://provider.com/o/oauth2/token", + }, + } + + // Exchange request will be made by the custom + // HTTP client, hc. + _, err := conf.Exchange(ctx, "foo") + if err != nil { + log.Fatal(err) + } +}