Share your feedback about the Google Ads (AdWords) API. Take the 2021 AdWords API and Google Ads API Annual Survey.

OAuth Web Application Flow

This guide will walk you through how to setup OAuth2 for API access using your own credentials using web application flow. These steps only need to be done once, unless you revoke, delete, or need to change the allowed scopes for your OAuth2 credentials.

Step 1 - Creating OAuth2 credentials

Generate a client ID and secret by following the linked instructions, then come back to this page. If you're using the authenticate_in_web_application example, you'll need to add localhost:8080 to the list of authorized redirect URIs when creating your new credentials.

Once finished save your client ID and client secret by going back to the credentials page and clicking the button on the right-hand side of the screen to download your credentials as a JSON file. Save that file on your machine in a location that's easy to remember, such as in your $HOME directory.

Step 2 - Adding OAuth2 credentials to configuration

Set the client ID and client secret tokens in your configuration. If you're using a google-ads.yaml file, YAML string or dict add the following:

client_id: OAUTH2_CLIENT_ID
client_secret: OAUTH2_CLIENT_SECRET

If you're using environment variables add the following to your Bash configuration or environment:

export GOOGLE_ADS_CLIENT_ID=OAUTH2_CLIENT_ID
export GOOGLE_ADS_CLIENT_SECRET=OAUTH2_CLIENT_SECRET

Step 3 - Generating a refresh token

  1. Follow the instructions in our installation documentation to clone the library from GitHub and return to this page.

  2. Next, navigate to the authenticate_in_web_application example, authenticate_in_web_application.py.

    $ cd examples/authentication
    
  3. Run this example using the command line, passing in the file path to the JSON file generated earlier.

    $ ./authenticate_in_web_application.py --client_secrets_path=/path/to/secrets.json
    
  4. The example will prompt you to visit a URL where you will need to allow the OAuth2 credentials to access your Google Ads account on your behalf.

    • Navigate to the URL in a private browser session or an incognito window.
    • Sign in with the same Google account you use to access Google Ads.
    • Click Allow on the OAuth2 consent screen.

    Consent screen allow

  5. After printing the above URL to the console, the example will open a socket to receive an HTTP request from the Google Auth servers, which contains an authorization code. It will parse that code and use it to generate a refresh token, which the example will print directly to the console.

Step 4 - Adding refresh token to configuration

Set the refresh token in your configuration. If you're using a google-ads.yaml file, YAML string or dict add the following:

refresh_token: OAUTH2_REFRESH_TOKEN

If you're using environment variables add the following to your Bash configuration or environment:

export GOOGLE_ADS_REFRESH_TOKEN=OAUTH2_REFRESH_TOKEN