set up Google Ads APIs

tl;dr: create all Google Ads auth components so you can automate Google & Youtube Ads

cost: $0 (you don't necessarily need to run any ads, either)

build time: 60 minutes

before getting into it, one (or two) disclaimer(s):

There are two Google ad-related API suites: the old Adwords API and the new Google Ads API.

The new Google Ads API is generally preferable, as the old one will eventually be deprecated (the reps said "in a couple years"). However, it is slower, harder to use, and was put back into beta after launching because it underperformed the old Adwords API 😂

Some components work for both (e.g. auth tokens). Some components are pretty different. I'll try to be clear where the differences are.

Disclaimer 2: The Google Adwords/Ads API structure, documentation, and examples are a convoluted mess of contradictory advice, dead links, and labyrinthine machine-generated text. Feel free to reach out to me at alec@contextify.io if you get stuck.

overview

To set the auth up, you'll need to:

  1. (if you don't have one) create an Adwords account
  2. create an Adwords Manager Account
  3. create a new project in Google API Console
  4. create a new OAuth ClientID in that project
  5. generate a refresh token
  6. assemble environment variable and test

You will need a domain that A) you own and B) you have Google Suite (email sending) attached to. Make sure each step (Adwords account, API Console project, OAuth authentication) are all done with the same email.


#1 - create an Adwords account

#1.1 - go here and click Sign Up For Google Ads

#1.2 - go through the signup wizard

  • enter Goal
  • choose name + site (has to be a live site)
  • set location
  • set category
  • write an example ad
  • set the minimum budget (1.65/day)
  • review
  • use a coupon to cover initial ad spend (check their Facebook ads to see if there are any promos in your country or use this link)

#1.3 - switch to Expert Mode

#1.4 - pause your only campaign (if you don't want to spend yet)

#1.5 - never use Smart Campaigns

If you do decide to run ads (or you are currently), make 100% sure you are not using Smart Campaigns (they are set as the default when you create a new account). They are dumbed down garbage that will waste your money and hide your data.


#2 - go here to create a Manager account

#2.1 - go to the API Center and apply for a developer token

#2.2 - apply for Basic Access

They make it sound like the Testing level is useful, but it's not. Apply for Basic here.

#2.3 - get the now-generated token


#3 - go here to create a Google API Console project

#3.1 - enable the Adwords API


#4 - create an OAuth client ID

#4.1 - configure consent screen

#4.2 - choose internal facing + redirect URI

(after setting up the consent screen you'll be sent back to the project page. Do the step in 4.0 again)

Pick internal facing and add the redirect URI to the domain you own


#5 - generate refresh token

#5.1 - download generate_refresh_token.py from the Googleads Github repo

curl https://raw.githubusercontent.com/googleads/googleads-python-lib/master/examples/adwords/authentication/generate_refresh_token.py >> generate_refresh_token.py

#5.2 - edit the redirect URI in generate_refresh_token.py to match the one you authorized in the Google API Console

_REDIRECT_URI = "https://www.alec.fyi" # formerly: 'urn:ietf:wg:oauth:2.0:oob'

#5.3 - initiate creating a refresh token

Using the client_id and client_secret you got from creating the Oauth client ID in the Google API Console, invoke the generate_refresh_token.py script:

python generate_refresh_token.py --client_id INSERT_CLIENT_ID --client_secret INSERT_CLIENT_SECRET

#5.4 - go to OAuth link and authenticate using your Adwords/API Console email

#5.5 - collect the verification code from the redirect URL and paste that back into the CLI to get your refresh_token

After approving the token enter the verification code (if specified).
Code:
4/uSmiFKlYzJ41BSrsyL4bZLmLRCNSLcPDCvSQyZaERwH42SDDAMK4yG0daKHP6AEJgquGUtlMhlkY5LHx0cn9mtEq3vH


#6 - assemble env var and test

#6.1 - here's a sample constructed auth (the "tokens" are random strings of the correct length)

export GOOGLEADS_SECRET='{"developer_token":"nyTZ7qGQ1AjrYa4kF-E23Q","client_customer_id":"0966023613","client_id":"n6niRQNedk8BX-tT2lGoHzejqAf4jkrETKdwk4EeVg0Qr0.apps.googleusercontent.com","client_secret":"K-wUoOAeoxIUAxDvQL_Vuf3A","refresh_token":"1//nFtvooivBOKHBhG3XJpJbitJxlBNRLS5nzdmrAu1wci9DBnYLk60vpIlEcvYpfA7CjZV7EZF9rSWKg5s6r9aadHHeGefqp07X7eX"}'

You can then load this JSON string into any application and use it

#6.2 - using it

To authenticate with the old Adwords API, using environment variables:

from googleads import adwords, oauth2, errors
import json

def auth_adwords():
    auth_dict = json.loads(os.environ["GOOGLEADS_SECRET"])
    oauth2_client = oauth2.GoogleRefreshTokenClient(auth_dict["client_id"], auth_dict["client_secret"], auth_dict["refresh_token"])
    adwords_client = adwords.AdWordsClient(auth_dict["developer_token"], oauth2_client, "Alec-ETL", client_customer_id=auth_dict["client_customer_id"])
    return adwords_client, auth_dict["client_customer_id"]

To authenticate with the new Google Ads API, using environment variables:

from google.ads.google_ads.client import GoogleAdsClient
import json
import os

def auth_google_ads():

    auth_dict = json.loads(os.environ["GOOGLEADS_SECRET"])
    for k,v in auth_dict.items():
        os.environ["GOOGLE_ADS_" + k.upper()] = v

    ads_client = GoogleAdsClient.load_from_env()
    return ads_client, auth_dict["client_customer_id"]


Thanks for reading. Questions or comments? 👉🏻 alec@contextify.io