...
1
2
3
4
5 package impersonate_test
6
7 import (
8 "context"
9 "log"
10
11 admin "google.golang.org/api/admin/directory/v1"
12 "google.golang.org/api/impersonate"
13 "google.golang.org/api/option"
14 "google.golang.org/api/secretmanager/v1"
15 "google.golang.org/api/transport"
16 )
17
18 func ExampleCredentialsTokenSource_serviceAccount() {
19 ctx := context.Background()
20
21
22 ts, err := impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{
23 TargetPrincipal: "foo@project-id.iam.gserviceaccount.com",
24 Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
25
26 Delegates: []string{"bar@project-id.iam.gserviceaccount.com"},
27 })
28 if err != nil {
29 log.Fatal(err)
30 }
31
32
33
34 client, err := secretmanager.NewService(ctx, option.WithTokenSource(ts))
35 if err != nil {
36 log.Fatal(err)
37 }
38
39
40
41 client.Projects.Secrets.Get("...")
42 }
43
44 func ExampleCredentialsTokenSource_adminUser() {
45 ctx := context.Background()
46
47
48 ts, err := impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{
49 TargetPrincipal: "foo@project-id.iam.gserviceaccount.com",
50 Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
51
52 Delegates: []string{"bar@project-id.iam.gserviceaccount.com"},
53
54 Subject: "admin@example.com",
55 })
56 if err != nil {
57 log.Fatal(err)
58 }
59
60
61
62 client, err := admin.NewService(ctx, option.WithTokenSource(ts))
63 if err != nil {
64 log.Fatal(err)
65 }
66
67
68
69 client.Groups.Delete("...")
70 }
71
72 func ExampleIDTokenSource() {
73 ctx := context.Background()
74
75
76 ts, err := impersonate.IDTokenSource(ctx, impersonate.IDTokenConfig{
77 Audience: "http://example.com/",
78 TargetPrincipal: "foo@project-id.iam.gserviceaccount.com",
79 IncludeEmail: true,
80
81 Delegates: []string{"bar@project-id.iam.gserviceaccount.com"},
82 })
83 if err != nil {
84 log.Fatal(err)
85 }
86
87
88
89 client, _, err := transport.NewHTTPClient(ctx, option.WithTokenSource(ts))
90 if err != nil {
91 log.Fatal(err)
92 }
93
94
95
96 client.Get("http://example.com/")
97 }
98
View as plain text