@@ -48,9 +48,8 @@ func ExampleConfig() {
48
48
client .Get ("..." )
49
49
}
50
50
51
- func ExampleCustomHTTP () {
52
- hc := & http.Client {Timeout : 2 * time .Second }
53
- ctx := context .WithValue (context .Background (), oauth2 .HTTPClient , hc )
51
+ func ExampleConfig_customHTTP () {
52
+ ctx := context .Background ()
54
53
55
54
conf := & oauth2.Config {
56
55
ClientID : "YOUR_CLIENT_ID" ,
@@ -62,19 +61,29 @@ func ExampleCustomHTTP() {
62
61
},
63
62
}
64
63
65
- tokenSource , err := conf .PasswordCredentialsToken (ctx , "YOUR_USERNAME" , "YOUR_PASSWORD" )
66
- if err != nil {
64
+ // Redirect user to consent page to ask for permission
65
+ // for the scopes specified above.
66
+ url := conf .AuthCodeURL ("state" , oauth2 .AccessTypeOffline )
67
+ fmt .Printf ("Visit the URL for the auth dialog: %v" , url )
68
+
69
+ // Use the authorization code that is pushed to the redirect
70
+ // URL. Exchange will do the handshake to retrieve the
71
+ // initial access token. The HTTP Client returned by
72
+ // conf.Client will refresh the token as necessary.
73
+ var code string
74
+ if _ , err := fmt .Scan (& code ); err != nil {
67
75
log .Fatal (err )
68
76
}
69
77
70
- // The returned client does not reuse
71
- // properties from the hc HTTP Client.
72
- client := oauth2 . NewClient (ctx , tokenSource )
78
+ // Use the custom HTTP client when requesting a token.
79
+ httpClient := & http. Client { Timeout : 2 * time . Second }
80
+ ctx = context . WithValue (ctx , oauth2 . HTTPClient , httpClient )
73
81
74
- resp , err := client . Get ( "http://www.example.com" )
82
+ tok , err := conf . Exchange ( ctx , code )
75
83
if err != nil {
76
84
log .Fatal (err )
77
85
}
78
86
79
- _ = resp // use the response
87
+ client := conf .Client (ctx , tok )
88
+ _ = client
80
89
}
0 commit comments