avatarPia Riachi

Summary

The provided content outlines the process for connecting to the Google Ads API using Python, including generating a developer token, configuring Google Cloud account and OAuth credentials, and testing the API with the Google Ads Python client library.

Abstract

The article is a comprehensive guide for advertisers and developers to programmatically access and manage Google Ads accounts through the Google Ads API. It details the necessary steps to obtain a developer token by creating a Google Ads Manager (MCC) account and generating the token within the Google Ads 'Tools & Settings' menu. The guide also covers the configuration of a Google Cloud project, enabling the Google Ads API, setting up OAuth credentials, and generating an OAuth refresh token using the Google OAuth Playground. Additionally, it instructs on installing the Google Ads Python module, creating a google-ads.yaml configuration file, and running a test script to verify API connectivity and functionality. The article emphasizes the importance of these steps for automating advertising processes, gathering insights, and creating custom solutions.

Opinions

  • The author, presumably a Google employee, indicates that the Google Ads API is a robust tool for automating advertising tasks and tailoring solutions to business needs.
  • The article suggests that having a Google Ads Manager account is essential for obtaining a developer token and managing multiple Google Ads accounts.
  • The author implies that the initial Test Access level for the developer token is insufficient for production accounts, which require Basic Access or Standard Access levels.
  • The guide recommends using the Google OAuth Playground for generating an OAuth refresh token, indicating its user-friendliness and convenience for developers.
  • The author provides a disclaimer that their posts and opinions are personal and not representative of Google's official stance.

How to Connect to the Google Ads API Using Python

Photo by Joshua Earle on Unsplash

The Google Ads API is a powerful tool that allows advertisers and developers to access and manage their Google Ads accounts and campaigns programatically. It enables you to automate processes, gather campaign insights, and create custom solutions tailored to your business needs. Working at Google, I get asked quite often for step by step instructions on how to connect to setup the Google Ads API.

In this blog post, I will walk you through the process of connecting to the Google Ads API so you can harness its potential and unlock new opportunities for your advertising endeavors. We will also walk through testing the API using the Google Ads API python client library.

Generate a Developer Token

1- Create a Google Ads Manager (MCC) Account

In order to generate a developer token, you would need a Google Ads Manager account. Create a Google Ads Manager account via https://ads.google.com/home/tools/manager-accounts

Google Ads Manager accounts allow you to access multiple Google Ads accounts including other manager accounts, manage their campaigns, and generate Developer Tokens. The manager account holds the developer token and does not dictate which accounts can the developer token can access. After creating the manager account (unless you already have one) you could then create a test account, or use a regular account with some paused or enabled campaigns that we can use later on for testing.

2- Generate a Google Ads Developer Token

Go to your Google Ads ‘Tools & Settings’ menu item, and click on API Center. There you will be able to see your developer token. The initial signup will generate a Test Access level. For production accounts, Basic Access or Standard Access levels must be requested.

Configure Your Google Cloud Account and OAuth credentials

1- Create a Google Cloud project

In case you do not already have a GCP project. Create one by visiting console.cloud.google.com/cloud-resource-manager, and clicking on ‘+CREATE PROJECT’.

2- Enable the Google Ads API in your project

In your GCP project, navigate to ‘APIs & Services’, and click on +Enable APIs & Services, search for the Google Ads API, and click Enable.

3- Configure OAuth credentials on GCP

OAuth2 lets your app operate on behalf of your account. Also under APIs & Services, navigate to Credentials, click on ‘+CREATE CREDENTIALS’, select OAuth Client ID. If this is your first time setting it up, you will be asked to configure the consent screen. Choose ‘Internal’ and press ‘Create’, fill in an App Name and user support email and developer contact information, and press ‘Save & Continue’.

Go back to ‘+CREATE CREDENTIALS’, choose Web Application, fill in a name, and use https://developers.google.com/oathplayground for the ‘Authorized redirect URI’, and the click on the ‘Create’ button.

Copy or download the created Client Id and Client secret values to a safe location.

4- Generate an OAuth refresh token

Navigate to https://developers.google.com/oathplayground, on the settings button (right upper corner on the time of writing this), select the checkbox ‘Use your own OAuth credentials’, and fill in your client id and client secret.

Select the Google Adwords API from the list on the left, and click on the Authorize APIs button. Select ‘Allow’, and select ‘Exchange authorization code for token’ to get your developer token.

You’re all done!

Let’s test it out:

Test your access

1- Install the Google Ads Python module

Please refer to the documentation on how to configure python on your machine in case it is not already installed. Then use the following command to install the google ads python module:

pip install google-ads

2- Create and Configure a google-ads.yaml config file

Create a google-ads.yaml file using the template found here: https://github.com/googleads/google-ads-python/blob/HEAD/google-ads.yaml

3- Run a test script to pull campaigns by lable (or any other test script).

This example script uses Google Ads Query language to retrieve campaigns by label, you could modify the query inside to retrieve all campaigns.

There are tons of examples on using the python api library with google ads here: https://github.com/googleads/google-ads-python/tree/main/examples

Add the google-ads-config.yaml file and the test script to the same folder and run the following command (assuming your test script is get_campaigns_by_label.py)

python get_campaigns_by_label.py

Disclaimer: My posts and opinions are my own, not those of my company (Google).

Google Ads Pi
Google Ads
Google Ads Python
Ads Api
Ads Api Python
Recommended from ReadMedium