OAuth Desktop and Web Application Flows

This guide walks you through how to set up OAuth2 for API access using your own credentials and either the desktop flow or the web flow. These steps only need to be done once, unless you revoke, delete, or need to change the allowed scopes for your OAuth2 credentials.

Create OAuth2 credentials

  1. Follow the steps to configure a Google API Console project for the Google Ads API.

  2. Note the client ID and secret, then come back to this page.

Set up the client library

  1. In a terminal, navigate to the OAuth2 example.

  2. Run this example using the command line. You can either modify the INSERT_XXX_HERE values in the example before running or use arguments -client_id for client ID and -client_secret for client secret.

    perl generate_user_credentials.pl -client_id {client_id} -client_secret {client_secret}
  3. The example prompts you to visit a URL where you'll need to allow the OAuth2 credentials to access your Google Ads account on your behalf.

    Paste this url in your browser:

    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 Continue on the OAuth2 consent screen.

    Consent screen

  4. You are then taken to a page with a message indicating that the authorization succeeded.

    Authorization code was successfully retrieved.

    You'll see that the example completed and your refresh token displayed with some instructions in the same page, followed by the properties you'll need to configure the client library:

    Replace the following keys and values in your googleads.properties configuration file:
  5. Return to the console where you're running the example, and press Ctrl + C to terminate the process. Then copy the generated refresh token along with the client ID and client secret into your googleads.properties or save it somewhere else to use if instantiating the library at runtime.