Google Ads API (AdWord API) Get Started

Get Started with Google Ads API | Google Ads API Setup – (AdWords API)

How to enable Google Ads API and generate credentials using google console?

Hi Hope you are doing well. 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 then will be enabling Google Ads API, next will be generating a token for OAuth Authentication through which we will be using Google Ads API to get Google Ads performance data.

What are 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 (AdWords 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.

NOTE: Google Ads API is the next-generation programming interface of the AdWords API. Currently, Google ads API is in beta, hence AdWords API will be soon replaced by Google Ads API.

In this article, we will be using the name Google Ads API rather than AdWords API.

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

This Guide will be covering the initial step of making software with Google Ads API to manage accounts.

Google Ads API (AdWord API) Get Started

Table of Content:

1~ Create Project in Google Console and Enable Google Ads API:

To access Google Ads account through API calls you to 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).

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 Create Google Console Projects.

a. Select Google Console Project –

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

b. 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 (easy way is to search for Google Ads API). 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 Google Ads API application to avoid confusion. Another suggestion is naming this application as “Google Ads”.

2~ Generate credentials and 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 Guide to create 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 Developer Token:

A developer token from google allows your app to connect to the Google Ads API. 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.

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"
}

4.2 Python code

#!/usr/local/bin/python3
#Python3 ./google_ads_main.py 
import sys
import json
from datetime import datetime, timedelta
 
from googleads import adwords
from googleads import oauth2
 
def google_ads_auth(client_id,client_secret,developer_token,refresh_token):
    try:
        #This is consistent for all Google services
        token_uri = 'https://accounts.google.com/o/oauth2/token'
 
        # Initialize the GoogleRefreshTokenClient using the credentials you received
        # in the earlier steps.
        oauth2_client = oauth2.GoogleRefreshTokenClient(
                        client_id, client_secret, refresh_token)
 
        # Initialize the AdWords client.
        adwords_client = adwords.AdWordsClient(
                        developer_token, oauth2_client)
 
        print("GA_AUTHENTICATION : Authentication process finished successfully")
        return adwords_client
 
    except:
        print("GA_AUTHENTICATION : Authentication process Failed %s ",sys.exc_info()[0])
 
def google_ads_account(client):
    # Initialize appropriate service.
    customers_service = client.GetService('CustomerService', version='v201809')
    customers_list = customers_service.getCustomers()
 
    for customer in customers_list:
        account_id = customer["customerId"]
        account_name = customer["descriptiveName"]
        currency_code = customer["currencyCode"]
        print("account_id :",account_id,"| account_name :",account_name, "| Currency Code :",currency_code)
 
if __name__ == '__main__':
    try:
        timestamp = datetime.strftime(datetime.now(),'%Y-%m-%d : %H:%M')
        print("DATE : ",timestamp)
        print("Google Ads process Started")
 
        #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_id = google_ads_cred["client_id"]
        client_secret = google_ads_cred["client_secret"]
        access_token = google_ads_cred["access_token"]
        refresh_token = google_ads_cred["refresh_token"]
        developer_token = google_ads_cred["developer_token"]
 
        client = google_ads_auth(client_id,client_secret,developer_token,refresh_token)
        
        google_ads_account(client)
        
    except:
        print(" Google Ads processing Failed :", sys.exc_info())
 

For additional help or more information, you can refer to Google AdWords API Documentation.

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 Google Ads API (AdWords API). Hope I have made the entire process of Getting Started with Google Ads API simple enough.

Since you have all credentials to build your software or project by integrating Google Ads API to retrieve Ads and campaign performance insight. Here’s a guide to help you out with Python Code to use Google Ads API to retrieve ads and campaign performance data.

Leave a Reply

Your email address will not be published. Required fields are marked *