Skip to content

[Deprecated, old library] API access using own credentials (installed application flow)

Thanet Knack Praneenararat edited this page Jul 20, 2017 · 2 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

  1. Open the Google Developers Console Credentials page.
  2. From the project drop-down, choose Create a new project, enter a name for the project, and optionally, edit the provided Project ID. Click Create.
  3. On the Credentials page, select Create credentials, then select OAuth client ID.
  4. You may be prompted to set a product name on the Consent screen; if so, click Configure consent screen, supply the requested information, and click Save to return to the Credentials screen.
  5. Select Other for the Application type, and enter any additional information required.
  6. Click Create.
  7. On the page that appears, copy the client ID and client secret to your clipboard. Client ID and secret

Step 2 - Setting up the client library

  1. Under the [OAUTH2] section of your auth.ini file, insert your client ID and secret.

    client_id = "INSERT_OAUTH2_CLIENT_ID_HERE"
    client_secret = "INSERT_OAUTH2_CLIENT_SECRET_HERE"
    
  2. In a terminal, navigate to the GetRefreshToken.php example for your product.

  3. 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 auth.ini file we edited in the step above. If your auth.ini file is not in the default location, you will need to edit the example to pass the path to your auth.ini when it constructs a new AdWordsUser or DfpUser.

    $ php GetRefreshToken.php
    Log in to your AdWords or 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
    
  4. The example will prompt you to visit a URL where you will need to allow the OAuth2 credentials to access your AdWords or DFP account 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

  5. An authorization code will be shown to you. Copy and paste the verification code into the command line where you're running the GetRefreshToken.php 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: ****
    
  6. Copy and paste this token into your auth.ini file where it says:

    refresh_token = "INSERT_OAUTH2_REFRESH_TOKEN_HERE"