OAuth Desktop Application Flow

This guide walks you through how to setup OAuth2 for API access using your own credentials in the desktop (installed) 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 - Create OAuth2 credentials

Generate a client ID and secret by following the linked instructions, then come back to this page.

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 - Add 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 shell configuration or environment:


Step 3 - Generate 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_desktop_application example, authenticate_in_desktop_application.py.

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

    $ ./authenticate_in_desktop_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. An authorization code will be shown to you. Copy and paste the verification code into the command line where you're running the authenticate_in_desktop_application.py example and press enter. The example should complete and display an offline refresh token.

    Authorization code

    After approving the token enter the authorization code here: ****
    Your refresh token is: ****

Step 4 - Add 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 shell configuration or environment: