...
1
2
3
4
5 package google_test
6
7 import (
8 "context"
9 "fmt"
10 "io/ioutil"
11 "log"
12 "net/http"
13
14 "golang.org/x/oauth2"
15 "golang.org/x/oauth2/google"
16 "golang.org/x/oauth2/jwt"
17 )
18
19 func ExampleDefaultClient() {
20 client, err := google.DefaultClient(oauth2.NoContext,
21 "https://www.googleapis.com/auth/devstorage.full_control")
22 if err != nil {
23 log.Fatal(err)
24 }
25 client.Get("...")
26 }
27
28 func Example_webServer() {
29
30
31 conf := &oauth2.Config{
32 ClientID: "YOUR_CLIENT_ID",
33 ClientSecret: "YOUR_CLIENT_SECRET",
34 RedirectURL: "YOUR_REDIRECT_URL",
35 Scopes: []string{
36 "https://www.googleapis.com/auth/bigquery",
37 "https://www.googleapis.com/auth/blogger",
38 },
39 Endpoint: google.Endpoint,
40 }
41
42
43 url := conf.AuthCodeURL("state")
44 fmt.Printf("Visit the URL for the auth dialog: %v", url)
45
46
47 tok, err := conf.Exchange(oauth2.NoContext, "authorization-code")
48 if err != nil {
49 log.Fatal(err)
50 }
51 client := conf.Client(oauth2.NoContext, tok)
52 client.Get("...")
53 }
54
55 func ExampleJWTConfigFromJSON() {
56
57
58
59
60
61
62
63 data, err := ioutil.ReadFile("/path/to/your-project-key.json")
64 if err != nil {
65 log.Fatal(err)
66 }
67 conf, err := google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/bigquery")
68 if err != nil {
69 log.Fatal(err)
70 }
71
72
73
74 client := conf.Client(oauth2.NoContext)
75 client.Get("...")
76 }
77
78 func ExampleSDKConfig() {
79
80
81 conf, err := google.NewSDKConfig("")
82 if err != nil {
83 log.Fatal(err)
84 }
85
86
87 client := conf.Client(oauth2.NoContext)
88 client.Get("...")
89 }
90
91 func Example_serviceAccount() {
92
93
94 conf := &jwt.Config{
95 Email: "xxx@developer.gserviceaccount.com",
96
97
98
99
100
101
102
103
104
105 PrivateKey: []byte("-----BEGIN RSA PRIVATE KEY-----..."),
106 Scopes: []string{
107 "https://www.googleapis.com/auth/bigquery",
108 "https://www.googleapis.com/auth/blogger",
109 },
110 TokenURL: google.JWTTokenURL,
111
112
113
114
115 Subject: "user@example.com",
116 }
117
118
119 client := conf.Client(oauth2.NoContext)
120 client.Get("...")
121 }
122
123 func ExampleComputeTokenSource() {
124 client := &http.Client{
125 Transport: &oauth2.Transport{
126
127
128
129
130
131 Source: google.ComputeTokenSource("", "https://www.googleapis.com/auth/bigquery"),
132 },
133 }
134 client.Get("...")
135 }
136
137 func ExampleCredentialsFromJSON() {
138 ctx := context.Background()
139 data, err := ioutil.ReadFile("/path/to/key-file.json")
140 if err != nil {
141 log.Fatal(err)
142 }
143 creds, err := google.CredentialsFromJSON(ctx, data, "https://www.googleapis.com/auth/bigquery")
144 if err != nil {
145 log.Fatal(err)
146 }
147 _ = creds
148 }
149
View as plain text