Navigating the Google Ads API Authorization Maze: Step-By-Step Guide for Authentication

Florian Grüning
kuwala-io
Published in
4 min readOct 5, 2023

The digital marketing world is a complex labyrinth and the Google Ads API is one of the bigger mystics. Often, developers and marketers find themselves in the endless journey of API authorization, a path that is seemingly straightforward, yet in practice, comes often with unexpected challenges.

In this guide, I aim to unveil the path for you, providing a structured, step-by-step guide to acquiring API access and obtaining the OAuth2 refresh token.

I also do this to enable you to use my Open-Source Repo which stores keyword ideas from the Adplanner automatically in a PostGres and helps you to analyze Search Data efficiently.

Note: You might started reading this article because you found my Github Repo … If not, check it out, since you should try my repo out once you have acquired your API Access.

For authorization you will need to acquire the following tokens and keys:

  • Register a Web-App using the Google Ads API on GCP (Google Cloud Platform) to receive a client_id and client_secret.
  • Acquire your Refresh Token through Google Ads API
  • Apply for an API Token in your Google Adplanner and find your customer_id

1. Google Ads API Access

The first step is fairly easy. We will just create a new App in your Google Cloud Platform and enable the API Service for Google Ads.

Setup a Google Cloud Project

  • Create a Project: Go to Google Cloud Console, and create a new project.
  • Enable API: Navigate to “API & Services” > “Dashboard” > “ENABLE APIS AND SERVICES”, and enable Google Ads API.
  • Create Credentials: Go to “API & Services” > “Credentials”, and create OAuth 2.0 client IDs. Download the JSON file with your credentials. Note: I have selected Web Application as the application type.
Step-By-Step setting up your Web Application in Google Cloud Platform

2. Obtain OAuth2 Refresh Token:

We will acquire the refresh token by spinning up a local development server. Once a refresh token acquired you can use it in your authentication process.

Start a local server

  • Create a file server.py and enter the following code:
from http.server import BaseHTTPRequestHandler, HTTPServer
import urllib.parse

class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
query_params = urllib.parse.urlparse(self.path).query
query_dict = urllib.parse.parse_qs(query_params)
auth_code = query_dict.get('code', [''])[0]

self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b"<html><body><h1>Received Authorization Code!</h1></body></html>")

print(f"Received Authorization Code: {auth_code}")

if __name__ == '__main__':
server_address = ('127.0.0.1', 8000)
httpd = HTTPServer(server_address, MyHandler)
print(f"Running server on {server_address[0]}:{server_address[1]}")
httpd.serve_forever()

Add an authorized redirect URI in GCP

  • Adjust in your Google Cloud Platform settings to Authorized redirect URIs with your server adress (http://127.0.0.1:8000)
Add your local development server into your web application in Google Cloud Platform

Receiving your refresh token and authenticate

  • pip install oauth2client
  • Create a short Authentication Script to get your refresh token. Please do not forget to adjust your CLIENT_ID and your CLIENT_SECRET which you acquired earlier in the process. Note: I have added the redirect_uri of this tutorial already. If you are using a different local server please adjust.
import sys
from oauth2client import client

# Replace these with your own client ID and secret.
CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLIENT_SECRET'

# The AdWords API OAuth 2.0 scope.
SCOPE = u'https://www.googleapis.com/auth/adwords'

def main():
"""Retrieve and display the access and refresh token."""
flow = client.OAuth2WebServerFlow(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope=[SCOPE],
user_agent='Ads Python Client Library',
redirect_uri='http://127.0.0.1:8000')

authorize_url = flow.step1_get_authorize_url()

print('Log into the Google Account you use to access your AdWords account'
' and go to the following URL: \n{}\n'.format(authorize_url))
print('After approving the token enter the verification code (if specified).')
code = input('Code: ').strip()

try:
credential = flow.step2_exchange(code)
except client.FlowExchangeError as e:
print('Authentication has failed: {}'.format(e))
sys.exit(1)
else:
print('OAuth 2.0 authorization successful!\n\n'
'Your access token is:\n {}\n\nYour refresh token is:\n {}'.format(
credential.access_token, credential.refresh_token))

if __name__ == '__main__':
main()

3. Access Google Ads API with Credentials:

Now the last step is to apply for a Google Ads API key. This is fairly easy but you need to understand that Google is very rigorous in the application process. More info can be found here. But here you have an overview of the different API key level:

  • Test: 15K requests per day / test environment
  • Basic: 15K requests per day / production environment
  • Standard: unlimited / production environment

My further tutorials will use a test account. This account is also fairly easy to acquire and apply for. Just follow the three steps withing your google ads account.

Acqusition of your Google API Key

4. Bring all your information together in a YAML File.

  • Name your file for example ‘google-ads.yaml’. This file will be used by the official python google ads library for authentication
  • Put in your developer_token (step 3), client_id (step 1), client_secret (step 1) and refresh_token (step 2).
developer_token: "your developer token"
client_id: "your client id"
client_secret: "your client secret"
refresh_token: "your refresh token"
use_proto_plus: True
  • Lastly, you also need your customer_id which is the little number next to your Manager account in Google Adplanner.

What Comes Next …

Now you have successfully authenticated your Google Ads API. You were able to authenticate through a web-app and put together all the credentials you need to interact with the google Ads API. In the next parts I will guide you through how to operate and interact with the Google Ads API — Stay tuned!

--

--

Florian Grüning
kuwala-io

I am all about content on how to enable fastly complete analytics workflows for companies 🚀