This project is a client library for the Google Ads API. Most of the code is generated from googleapis. And this project only adds some examples and minor adjustments. Please note this is an unofficial project.
- Fully support for Google Ads API.
- Support for GRPC calls.
- Support for HTTP calls by using protojson.
- Frequent updates based on official repository.
- Golang 1.4 or later.
- Before starting, recommend to read the OAuth2 guide.
- Apply for a developer token if you don't already have one.
go get -d github.com/shenzhencenter/google-ads-pb
- Set the environment variables.
export ACCESS_TOKEN=<your access token>
export DEVELOPER_TOKEN=<your developer token>
export CUSTOMER_ID=<your customer id>
- Create a GRPC connection.
ctx := context.Background()
headers := metadata.Pairs(
"authorization", "Bearer "+os.Getenv("ACCESS_TOKEN"),
"developer-token", os.Getenv("DEVELOPER_TOKEN"),
"login-customer-id", os.Getenv("CUSTOMER_ID"),
)
ctx = metadata.NewOutgoingContext(ctx, headers)
cred := grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, ""))
conn, err := grpc.Dial("googleads.googleapis.com:443", cred)
if err != nil { panic(err) }
defer conn.Close()
- Make the first call.
customerServiceClient := services.NewCustomerServiceClient(conn)
accessibleCustomers, err := customerServiceClient.ListAccessibleCustomers(
ctx,
&services.ListAccessibleCustomersRequest{},
)
if err != nil { panic(err) }
for _, customer := range accessibleCustomers.ResourceNames {
fmt.Println("ResourceName: " + customer)
}
- Make HTTP calls with using protojson.
// 4.1 Set request
listAccessibleCustomersRequest, err := protojson.Marshal(
&services.ListAccessibleCustomersRequest{},
)
if err != nil { panic(err) }
request, err := http.NewRequest(
"GET", "https://googleads.googleapis.com/v11/customers:listAccessibleCustomers",
bytes.NewBuffer(listAccessibleCustomersRequest))
if err != nil { panic(err) }
// 4.2 Set request headers
header := make(http.Header)
header.Set("authorization", os.Getenv("ACCESS_TOKEN"))
header.Set("developer-token", os.Getenv("DEVELOPER_TOKEN"))
header.Set("login-customer-id", os.Getenv("CUSTOMER_ID"))
request.Header = header
// 4.3 Send request
client := &http.Client{}
response, err := client.Do(request)
if err != nil { panic(err) }
defer response.Body.Close()
// 4.4 Read the response
var responseBody []byte
if responseBody, err = ioutil.ReadAll(response.Body); err != nil {
panic(err)
}
// 4.5 Print the response
listAccessibleCustomersResponse := new(services.ListAccessibleCustomersResponse)
if err := protojson.Unmarshal(responseBody, listAccessibleCustomersResponse); err != nil {
panic(err)
}
for _, customer := range listAccessibleCustomersResponse.ResourceNames {
fmt.Println("ResourceName: " + customer)
}
Here are some related projects
Welcome to contribute more examples and documentations.