forked from golang/oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
87 lines (73 loc) · 2.22 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package oauth2_test
import (
"context"
"fmt"
"log"
"net/http"
"golang.org/x/oauth2"
)
func ExampleConfig() {
ctx := context.Background()
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",
},
}
// Redirect user to consent page to ask for permission
// for the scopes specified above.
url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
fmt.Printf("Visit the URL for the auth dialog: %v", url)
// Use the authorization code that is pushed to the redirect
// URL. Exchange will do the handshake to retrieve the
// initial access token. The HTTP Client returned by
// conf.Client will refresh the token as necessary.
var code string
if _, err := fmt.Scan(&code); err != nil {
log.Fatal(err)
}
tok, err := conf.Exchange(ctx, code)
if err != nil {
log.Fatal(err)
}
client := conf.Client(ctx, tok)
client.Get("...")
}
func ExampleNewClient() {
customHTTPClient := &http.Client{
Timeout: time.Duration(10) * time.Seconds,
}
ctx := context.Background()
ctx = context.WithValue(ctx, oauth2.HTTPClient, customHTTPClient)
conf := &oauth2.Config{
ClientID: "YOUR_CLIENT_ID",
ClientSecret: "YOUR_CLIENT_SECRET",
Endpoint: oauth2.Endpoint{
TokenURL: "https://provider.com/o/oauth2/token",
},
}
// Use only if there is a high degree of trust between
// the resource owner and the client.
tokenSrc, err := conf.PasswordCredentialsToken(ctx, "YOUR_USERNAME", "YOUR_PASSWORD")
if err != nil {
log.Fatal(err)
}
// The Timeout configuration on the HTTP Client
// constructed above is used only during token
// acquisition and is not configured as part of
// the client returned from NewClient.
authedHTTPClient := oauth2.NewClient(ctx, tokenSrc)
response, err := authedHTTPClient.Get("http://www.example.com")
if err != nil {
log.Fatal(err)
}
if response.Code != http.StatusOK {
log.Fatal("response was not 200")
}
}