Using OAuth 2.0 to Access Google APIs

Google APIs use the OAuth 2.0 protocol for authentication and authorization. Google supports common OAuth 2.0 scenarios such as those for web server, installed, and client-side applications.

Step 1 - Register Application with Google Console API

  1. Log into Google using your email and password above
  2. Head to the Google API Console. You probably get redirected to Google Cloud Console
  3. Under 'APIs & Auth' click on 'Consent screen'. Fill in at least 'Product Name' and 'Email'.
  4. Under 'APIs & Auth' click on 'Credentials'.
  5. Click 'Create new Client ID'. Select 'Installed application' with type 'Other' as platform.
  6. Under 'Client ID for native application' make a note of the CLIENT ID and CLIENT SECRET values.

Step 2 - Generate Refresh Token

Next step is to generate a refresh token. This is a generate-once-use-multiple-times token that allows your application to obtain new access tokens:

  1. Download GetRefreshToken.java.
  2. Create an aps.properties file to be referenced by the GoogleClientSecretsBuilder().forApi(Api.ADWORDS) call. This ads.properties file should contain two lines:
    api.adwords.clientId=client-id-from-step1.6
    api.adwords.clientSecret=client-secret-from-step1.6
    
  3. Using web browser log into the Google AdWords MCC.
  4. Run GetRefreshToken.java and follow instructions i.e. copy browser URL into browser, enter code returned into console etc. etc.
  5. You should now have a refreshToken. Copy this refresh token into your ads.properties files like this:
    api.adwords.refreshToken=your-refresh-token
    
    Notes: GetRefreshToken.java has a couple of dependencies. If you are using Maven then here they are (adjust versions accordingly!):
    <dependency>
      <groupId>com.google.apis</groupId>
      <artifactId>google-api-services-oauth2</artifactId>
      <version>v2-rev50-1.17.0-rc</version>
    </dependency>
    <dependency>
      <groupId>com.google.api-ads</groupId>
      <artifactId>adwords-axis</artifactId>
      <version>1.20.0</version>
    </dependency>
    

Step 3 - Generate Credential

With your refreshToken, clientId & clientSecret in your ads.properties you can now generate a Credential like this:

Credential oAuth2Credential = new OfflineCredentials.Builder()
  .forApi(Api.ADWORDS).fromFile().build().generateCredential();

Step 4 - Get AdWords Session

The final step (hats off to you if you have got this far!) is to create an AdWords Session using the oAuth2Credential instance of Credential that you created in Step 3. You can do this by adding two more things into your ads.properties file:

api.adwords.developerToken=developer-token-from-mcc
api.adwords.clientCustomerId=client-id-of-adwords-account-that-you-want-to-access

Then get an AdWords session up using like this:

AdWordsSession awSession = new AdWordsSession.Builder().fromFile()
  .withOAuth2Credential(oAuth2Credential).build();

Step 5 - Grab a coffee and reflect on how easy it is to access the Google AdWords API using OAuth2

This step is entirely optional.

Comments

Popular posts from this blog

Reduce TIME_WAIT Socket Connections