Automating Google API with Anypoint Studio

Author: Sneha Veeranki

Google APIs is a set of application programming interfaces (APIs) developed by Google which allows communication with Google Services and their integration to other services.

It is useful for a third-party to take advantage of APIs and can even extend the functionalities of APIs. Examples of these include Search, YouTube, Google Spreadsheet, Google Maps, Google Contacts, etc.

To access the Google API we require an API key, client id, client secret, and the scope (the library that needs to be accessed, the scope can be found after enabling the API).

Using the client id, client secret, the scope, and authorize the user, and the access token can be generated.  The access token expires in an hour and to get another token the user needs to authorize manually, that’s when the refresh tokens come into the picture. 

Google Refresh Token allows you to generate access tokens when the user is offline.

The web applications store these refresh tokens so that the application can be authorized.

Using refresh tokens can avoid manually authorizing the application every time an access token is required.  

The refresh tokens never expire, unless-

  1. The refresh token has not been used for six months.
  2. The user changed the password and then the refresh token contained Gmail scopes. But this means the token is invalid only for Gmail and the token can be used for other Google applications. 
  3. The user has disabled the respective Google API.

Here, let’s see how to get the refresh token and use it to get an access token with an uninterrupted authorization of the Google API and how to enable any Google API, for instance, I have considered integration with google drive.     

We will see integration with Google Drive using a refresh token.

Step 1: Login to 

Step 2: Create a new project 

  • On the top click on the drop-down “Select a Project”
  • Click on “New Project”
  • Enter the project name and click “Create”  

Step 3: Create credentials (API key, OAuth 2.0 Client IDs)

  • Click on the options to top left 
  • Navigate to “APIs & Services” 
  • Click “Credentials” and create “API key”
  • API key gets created and the key can be restricted
  • Create “OAuth 2.0 Client IDs”, click on “Create Credentials” and navigate “OAuth 2.0 Client IDs”
  • Select “Configure content screen”, select “Internal” and click “Create”  
  • Enter the “Application Name” and click “save” at the bottom
  • Select “Other” and click “Create”

Step 4: Download the credentials JSON file, this file contains the client_id, client_secret, auth_uri and token_uri

Step 5: From options to left navigate to “Library”, search for “Google Drive API” and “Enable”

Step 6: Get the refresh token 

  • Open the JSON file that was downloaded earlier.
  • Open postman, select the “Authorization” tab beside the headers tab then choose OAuth2.0 from “Type” and click on “Get New Access Token”.
  • Enter the below information:
  • Token Name: <according to your choice>
  • Grant Type: Authorization Code
  • Callback URL: <redirect_uris> (“http://localhost/endponit”)

(sample: redirect_uris are given like this “endpoint”,”http://localhost“])

  • Auth URL: <auth_uri>
  • Access Token URL: <token_uri>
  • Client ID: <client_id>
  • Client Secret: <client_secret>
  • Scope: <Google API Scope> accordingly 
  • After entering all the details and click “Request Token”
  • Authorize the user, enter the email id and password, and then Access Token, Token Type, refresh token, and expires_in (within the given seconds the access token will be expired).  

We got the refresh token using the token let’s integrate the Google Drive with mulesoft to get access token with authorizing the user.

Step 7: Create a mule project

  • Open the anypoint studio File> New> Mule project 
  • Enter the project name and click “Finish”
  • Drag a “Listener (HTTP)” from the mule palette and drop it on the canvas.
  • Configure the “Listener” and enter the “Path”
  • Drag a “Request (HTTP)” and select the “Add” (green plus symbol on right), enter Host as “”, and click “Ok”
  • In the Request set the “Method” to POST and “Path” to “/o/oauth2/token”  
  • Then set the “Body” as,
“%dw 2.0
output application/json
"grant_type": "refresh_token",
    "refresh_token":<refresh token>,
  • Open the “Headers” tab, click on Add, and enter “Name” to “Content-Type” and “Value” to “application/json”.   
  • Add a “Transform message” and change the output to application/json and map it to Payload and Run the application.
  • Once the application is deployed, open the postman, enter the URL (sample: “http://localhost:8081/googleDriveTest”), and click send

In the above image, we acquired the access token. Whenever we need an access token we can hit the endpoint and can use it throughout the application to authorize the Google API. We also learnt how to enable the Google Drive API.  

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.