-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (78 loc) · 2.01 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"context"
"encoding/json"
"errors"
"log"
"os"
"github.com/joeshaw/envdecode"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/iterator"
"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"`
LoginCustomerID string `env:"LOGIN_CUSTOMER_ID,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)
ctx = metadata.AppendToOutgoingContext(ctx, "login-customer-id", env.LoginCustomerID)
client, err := googleads.NewClient(ctx, opts...)
if err != nil {
log.Fatalln(err)
}
req := &servicespb.SearchGoogleAdsRequest{
CustomerId: env.LoginCustomerID,
Query: query,
}
it := client.Search(ctx, req)
enc := json.NewEncoder(os.Stdout)
for {
row, err := it.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
log.Fatalln(err)
}
if err := enc.Encode(row); err != nil {
log.Fatalln(err)
}
}
}
const query = `
SELECT
customer_client.id,
customer_client.descriptive_name,
customer_client.manager
FROM
customer_client
`