Skip to content
This repository has been archived by the owner on Dec 14, 2022. It is now read-only.

API access using own credentials (installed application flow)

sundquist edited this page Sep 20, 2018 · 4 revisions

This guide will walk you through how to setup OAuth2 for API access using your own credentials using 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 - Creating OAuth2 credentials

Follow the steps to generate a client ID and secret, then come back to this page.

Step 2 - Setting up the client library

  • Under the OAuth2 section of your adwords.properties file, insert your client ID and secret.

    oAuth2ClientId=INSERT_OAUTH_CLIENT_ID_HERE
    oAuth2ClientSecret=INSERT_OAUTH_CLIENT_SECRET_HERE
    
  • In a terminal, navigate to the directory of the examples/oauth/generate_offline_credentials.pl example.

  • Run this example via the command line.

    This is an interactive example, which will require you to provide input. It will not run properly in a web browser. This example will read the client ID and secret from the adwords.properties file we edited in the step above. If your adwords.properties file is not in the default location, you will need to put it there. For Linux, that would be ~/adwords.properties.

    $ perl generate_offline_credentials.pl
    Log in to your Google Ads account and open the following URL:
    https://accounts.google.com/o/oauth2/authresponse_type=code&client_id=****&redirect_uri=****&scope=****&access_type=offline
    
  • The example will prompt you to visit a URL where you will need to allow the OAuth2 credentials to access your Google Ads network on your behalf. Navigate to the URL in a private browser session or an incognito window. Log in with the same Google account you use to access Google Ads. Click Allow on the OAuth2 consent screen.

    Consent screen allow

  • An authorization code will be shown to you. Copy and paste the verification code into the command line where you're running the generate_offline_credentials.pl example and press enter. The example should complete and display an offline refresh token.

    Authorization code

    Grant access to the applications and enter the authorization code display in the page then hit ENTER.
    Enter confirmation code: ***
    
    The following are the keys you can replace in your adwords.properties configuration:
    
    oAuth2ClientId=***
    oAuth2ClientSecret=***
    oAuth2AccessToken=***
    oAuth2RefreshToken=***
    
  • Copy and paste these tokens into your adwords.properties file where it says:

    oAuth2RefreshToken=INSERT_OAUTH_TOKEN_HERE
    

Note: The oAuth2AccessToken can be set in the adwords.properties file, but it will expire in one hour if it is set. To have the access token refresh automatically, only set oAuth2RefreshToken, and comment out oAuth2AccessToken.