...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package credentials_test
16
17 import (
18 "log"
19 "os"
20
21 "cloud.google.com/go/auth/credentials"
22 "cloud.google.com/go/auth/httptransport"
23 )
24
25 func ExampleDetectDefault() {
26 creds, err := credentials.DetectDefault(&credentials.DetectOptions{
27 Scopes: []string{"https://www.googleapis.com/auth/devstorage.full_control"},
28 })
29 if err != nil {
30 log.Fatal(err)
31 }
32 client, err := httptransport.NewClient(&httptransport.Options{
33 Credentials: creds,
34 })
35 if err != nil {
36 log.Fatal(err)
37 }
38 client.Get("...")
39 }
40
41 func ExampleDetectDefault_withFilepath() {
42
43
44
45
46
47
48
49 filepath := "/path/to/your-project-key.json"
50 creds, err := credentials.DetectDefault(&credentials.DetectOptions{
51 Scopes: []string{"https://www.googleapis.com/auth/bigquery"},
52 CredentialsFile: filepath,
53 })
54 if err != nil {
55 log.Fatal(err)
56 }
57 client, err := httptransport.NewClient(&httptransport.Options{
58 Credentials: creds,
59 })
60 if err != nil {
61 log.Fatal(err)
62 }
63 client.Get("...")
64 }
65
66 func ExampleDetectDefault_withJSON() {
67 data, err := os.ReadFile("/path/to/key-file.json")
68 if err != nil {
69 log.Fatal(err)
70 }
71 creds, err := credentials.DetectDefault(&credentials.DetectOptions{
72 Scopes: []string{"https://www.googleapis.com/auth/bigquery"},
73 CredentialsJSON: data,
74 })
75 if err != nil {
76 log.Fatal(err)
77 }
78 client, err := httptransport.NewClient(&httptransport.Options{
79 Credentials: creds,
80 })
81 if err != nil {
82 log.Fatal(err)
83 }
84 client.Get("...")
85 }
86
View as plain text