Google Ads API (AdWord API) Get Started

Google Ads API Setup Guide – Get Started with Google Ads API

Enable Google Ads API and generate credentials using Google console – Step-by-step guide

Hi, Hope you are doing well. Thank you so much for stopping by. It’s another day to learn something fruitful. To give you an idea of what we will be learning here. The guide will help you in creating Google console projects, we will be enabling Google Ads API, next we will be generating tokens (access token, client id, etc) for OAuth Authentication through which we will be using Google Ads API to get Google Ads and Campaigns performance data.

What is Google Ads?

Google Ads is an online advertising platform developed by Google, where advertisers bid to display brief advertisements, service offerings, product listings, or videos to web users. For more details visit Link.

What is Google Ads API?

The API provided by Google enables interaction with the Google Ads platform directly. It makes it easy and efficient to manage large or complex Google Ads accounts and campaigns.

A developer can easily build software with this API. With help of API, one can automate account management, custom reporting, ad management, and much more functionality. The API can do everything the Google Ads UI does but programmatically.

Google Ads API (AdWord API) Get Started

1. Create a Project in Google Console and Enable API

To access a Google Ads account through API calls, you need to turn on Google Ads API first. In order to do that go to Google Console, create an app or select an app (If you already have a project/application created in google console).

Are you finding it difficult and confusing to create a project or App in google console? Don’t worry we got you covered – here’s a Detail Guide to Creating Google Console Projects.

1.1. Select Google Console Project

As you can see from the image below, I have selected “Google Ads” (that’s my application or project name).

1.2. Search and Enable API

To enable the API, confirm you are in the “Dashboard” view and look out for the “ENABLE APIS and SERVICES” button and click on it. Hope you find it soon!

Look for Google Ads API (an easy way is to search for it). Click on it and enable the API for your project by clicking on the “ENABLE” button.

I recommend creating a new app under google console, specific to your application to avoid confusion. Another suggestion is to name this application “Google Ads”.

2. Generate project credentials and OAuth-related tokens

After you have enabled the API for your project it’s time to generate credentials for your project so that you can use them from the application you are building. To do so refer to the 3rd step of the Guide to Creating Google Console Project.

Suggestion: name this credential after the type of API or Application. In this case for example it will be google_ads_api_cred. Don’t forget to keep the note of the client id and client secret you will get at the end of the 3rd step. That means you should now have client_id= XXXXXXXX and Client_secret = XXXXXXXX.

Next, we will see how to generate tokens. For this again we will refer to the  Guide to create Google Console Project step 4. This is the most important process to generate a token for OAuth verification. (NOTE: There are 2 ways mentioned in step 4, you can choose whichever suits you best and meets the requirement).

3. Get a Developer Token

A developer token from Google allows your app to connect to Google Ads. To get your developer token sign in to your Manager Account. (note: you should be signed in as a Google Ads Manager Account).

Navigate to TOOLS & SETTINGS > SETUP > API Centre. You should see a developer token or you will see an API access form (you need to fill up the form and complete the entire process to get the developer token).

The API Centre option will appear only for Google Ads Manager Accounts.

NOTE: Developer token must be approved before using it with production Google Ads accounts. If you are stuck here and not able to figure out any solution, I recommend you should get some help from the detailed Sign-Up Guide.

If you want I can help you out with a python code to generate an access token and the entire process automated. Get in touch with me. This all can be done in 1 hour for you.

4. Python code to authenticate and make the first API call

4.1 Create a JSON file

{
    "client_id":"Replace with Client Id",
    "client_secret":"Replace with Client Secret",
    "access_token":"Replace with Access Token",
    "refresh_token":"Replace with Refresh Token",
    "developer_token" :"Replace with Developer Token",
    "version":"v10"
}

4.2 Python code

#!/usr/local/bin/python3
#python3 ./google_ads_first_api_call.py 
import sys
import json
import time
from datetime import datetime
import pandas as pd
from google.ads.googleads.client import GoogleAdsClient


def google_ads_authentication(token):
    try:
        google_ads_client = GoogleAdsClient.load_from_dict(token)
        print("\nFunction (google_ads_authentication) - authentication process finished successfully ")

        return google_ads_client

    except:
        print("\n*** Function (google_ads_authentication) Failed *** ",sys.exc_info())

def get_google_ads_customer(client):

    customer_service = client.get_service("CustomerService")
    customer_resource_names = (customer_service.list_accessible_customers().resource_names)

    customer_id_list = []

    googleads_service = client.get_service("GoogleAdsService")

    for customer_resource_name in customer_resource_names:
        customer_id = googleads_service.parse_customer_path(customer_resource_name)["customer_id"]
        customer_id_list.append(customer_id)
    
    #print("\n customer_id_list : ",customer_id_list)
    
    query = """
        SELECT customer_client.client_customer, customer_client.level, customer_client.manager,
            customer_client.descriptive_name, customer_client.currency_code, customer_client.time_zone, customer_client.id,
            customer_client.test_account
        FROM customer_client
        WHERE customer_client.level <= 1 and customer_client.test_account = 'false'
        """

    #and customer.test_account != TRUE PARAMETERS include_drafts=true

    final_list=[]
    for c in customer_id_list:
        try:
            response = googleads_service.search(customer_id=str(c), query=query)
            acc = None
            for d in response:
                customer_client = d.customer_client
                #print("\ncustomer detail : ",customer_client)
                
                if customer_client.level == 0:
                    if acc is None:
                        acc = customer_client
                        tmp = {"id":acc.id,"name":acc.descriptive_name, "time_zone":acc.time_zone}
                        final_list.append(tmp)
                # else:
                #     print("\n level not 0 ",customer_client)
        except:
            pass
    
    account_dt = pd.DataFrame(final_list)
    return account_dt



if __name__ == '__main__':
    try:
        timestamp = datetime.strftime(datetime.now(),'%Y-%m-%d : %H:%M')
        print("Get Google Ads account details process Started at ",datetime.now())
        start = time.process_time()

        #Read the Credentials from the JSON file.
        cred_file = "./google_ads_cred.json"
        cred_data = open(cred_file, 'r')
        google_ads_cred = json.load(cred_data)

        client = google_ads_authentication(google_ads_cred)
        
        accounts_details_df =  get_google_ads_customer(client)
        print("\n Account Details :\n",accounts_details_df)

        print("\nGet Google Ads account details process ends at ",datetime.now())
        print("Processing Time : ",(time.process_time()-start))
    except:
        print("\n *** Get Google Ads account processing Failed :", sys.exc_info())

Congratulations! You have successfully set up and got all the credentials needed to authenticate your application on behalf of the user so that you can use the above API. Hope I have made the entire process of Getting Started with Google Ads API simple enough. Here’s a guide to help you with Python Code using Google Ads API to retrieve ads and campaign performance data.

Hope I was able to solve the problem. If you like this article and think it was easy to understand do share it with your friends and connection. Thank you! See you soon.

For any suggestions or doubts ~ Get In Touch

Checkout out my other API Integration and Coding Solution Guide