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
  2. Create an file to be referenced by the GoogleClientSecretsBuilder().forApi(Api.ADWORDS) call. This file should contain two lines:
  3. Using web browser log into the Google AdWords MCC.
  4. Run 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 files like this:
    Notes: has a couple of dependencies. If you are using Maven then here they are (adjust versions accordingly!):

Step 3 - Generate Credential

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

Credential oAuth2Credential = new OfflineCredentials.Builder()

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 file:


Then get an AdWords session up using like this:

AdWordsSession awSession = new AdWordsSession.Builder().fromFile()

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.


Popular posts from this blog