google-ads-pb

module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2022 License: Apache-2.0

README

Google Ads API Client Library for Golang

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.

Features

  • Fully support for Google Ads API.
  • Support for GRPC calls.
  • Support for HTTP calls by using protojson.
  • Frequent updates based on official repository.

Requirements

Installation

go get -d github.com/shenzhencenter/google-ads-pb

Getting started

  1. Set the environment variables.
export ACCESS_TOKEN=<your access token>
export DEVELOPER_TOKEN=<your developer token>
export CUSTOMER_ID=<your customer id>
  1. 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()
  1. 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)
}
  1. 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)
}

Examples

Account management
Campaign management

Here are some related projects

Contributing

Welcome to contribute more examples and documentations.

Directories

Path Synopsis
Package clients is an auto-generated package for the Google Ads API.
Package clients is an auto-generated package for the Google Ads API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL