Skip to content

API access using own credentials (installed application flow)

nicholaschen edited this page May 26, 2016 · 8 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 for the product you're using to generate a client ID and secret, then come back to this page.

Step 2 - Setting up the client library

  1. Under the authentication section of your adwords_api.yml or dfp_api.yml file, insert your client ID and secret.
:oauth2_client_id: INSERT_OAUTH2_CLIENT_ID_HERE
:oauth2_client_secret: INSERT_OAUTH2_CLIENT_SECRET_HERE
  1. In a terminal, navigate to the OAuth2 example for your product.
  1. 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 configuration file we edited in the step above. If your configuration file is not in the default location, you will need to edit the example to pass the path to your configuration file when it constructs a new AdwordsApi::Api or DfpApi::Api.

$ ruby setup_oauth2.rb
Log in to your Adwords/DFP account and open the following URL:
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=****&redirect_uri=****&scope=****&access_type=offline
  1. The example will prompt you to visit a URL where you will need to allow the OAuth2 credentials to access your AdWords account or DFP 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 AdWords or DFP. Click Allow on the OAuth2 consent screen.

Consent screen allow

  1. An authorization code will be shown to you. Copy and paste the verification code into the command line where you're running the setup_oauth2.rb 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: ****
  1. Allow the example to update your config file automatically.