-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (114 loc) · 2.83 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"os"
"strconv"
"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"`
}
var (
accountID = flag.Int("account", 0, "ad account id (required)")
campaignID = flag.Int("campaign", 0, "campaign id")
)
func main() {
err := envdecode.Decode(&env)
if err != nil {
log.Fatalln(err)
}
flag.Parse()
if *accountID == 0 {
flag.PrintDefaults()
os.Exit(1)
}
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)
if *campaignID > 0 {
show(ctx, *campaignID, opts)
return
}
list(ctx, *accountID, opts)
}
func show(ctx context.Context, id int, opts []option.ClientOption) {
client, err := googleads.NewCampaignClient(ctx, opts...)
if err != nil {
log.Fatalln(err)
}
req := &servicespb.GetCampaignRequest{
ResourceName: fmt.Sprintf("customers/%d/campaigns/%d", *accountID, *campaignID),
}
res, err := client.GetCampaign(ctx, req)
if err != nil {
log.Fatalln(err)
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(res); err != nil {
log.Fatalln(err)
}
}
func list(ctx context.Context, id int, opts []option.ClientOption) {
client, err := googleads.NewClient(ctx, opts...)
if err != nil {
log.Fatalln(err)
}
req := &servicespb.SearchGoogleAdsRequest{
CustomerId: strconv.Itoa(id),
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
campaign.id,
campaign.name
FROM
campaign
`