-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (55 loc) · 1.57 KB
/
main.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
package main
import (
"context"
"log"
"os"
"strings"
"github.com/joeshaw/envdecode"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/grpc/metadata"
servicespb "github.com/scotthenley/go-googleads/pb/v9/services"
googleads "github.com/scotthenley/go-googleads/v9"
)
const (
gadsAddr = "googleads.googleapis.com:443"
scope = "https://www.googleapis.com/auth/adwords"
)
var env struct {
ClientID string `env:"CLIENT_ID,required"`
ClientSecret string `env:"CLIENT_SECRET,required"`
RefreshToken string `env:"REFRESH_TOKEN,required"`
DeveloperToken string `env:"GADS_DEVELOPER_TOKEN,required"`
}
func main() {
err := envdecode.Decode(&env)
if err != nil {
log.Fatalln(err)
}
ctx := context.Background()
t := &oauth2.Token{RefreshToken: env.RefreshToken, TokenType: "Bearer"}
conf := &oauth2.Config{
ClientID: env.ClientID,
ClientSecret: env.ClientSecret,
Scopes: []string{scope},
Endpoint: google.Endpoint,
RedirectURL: "http://localhost:8080",
}
opts := []option.ClientOption{
option.WithTokenSource(conf.TokenSource(ctx, t)),
option.WithEndpoint(gadsAddr),
}
ctx = metadata.AppendToOutgoingContext(ctx, "developer-token", env.DeveloperToken)
client, err := googleads.NewCustomerClient(ctx, opts...)
if err != nil {
log.Fatalln(err)
}
req := servicespb.ListAccessibleCustomersRequest{}
res, err := client.ListAccessibleCustomers(ctx, &req)
if err != nil {
log.Fatalln(err)
}
names := strings.Join(res.GetResourceNames(), "\n")
os.Stdout.WriteString(names)
}