MuleSoft: Using the Anypoint Google Drive Connector in an application

Haleema Khan
5 min readFeb 23, 2022

Anypoint Connector for Google Drive enables you to integrate your application with google drive and perform various functions that can be used to drive businesses. Files can be uploaded directly to google drive along with modifying the metadata attached to them.

Prerequisites

-Running Anypoint studio setup

-Mule Runtime Engine

-Google account

-Subscription to Google Drive API

Current Use-Case

This is a basic quick start guide on integrating your application with the google drive connecter. This includes:

-Google drive connector configuration

-OAuth setup for local/CloudHub

-Folder and File creation on drive

-Manipulating metadata of a file

Google Drive API subscription

Go to Google Cloud SignUp

-Go to API & Services and open the Google Drive API from the list at the end

-Enable the API ( A project needs to be in place for a subscription that can be created)

-Go to credentials

-Create credentials for an Oauth2 Client

-Copy the client Id and consumer key that is generated

Google Drive Connector Configuration

The basic configuration includes a consumer key, consumer secret that a user needs to add. This is obtained in the last step.

Once this is done, you have to add a listener configuration for Oauth Callback.

If you are working on local just add :

For deploying applications on cloudHub use the default host (0.0.0.0). This will be resolved on runtime to the correct URL.

Note: Leave the external callback empty for running on local. For CloudHub, set the URL as HTTP://{domainURL}/callback

OAuth Dance

For successfully running your application, you need to perform the OAuth Dance once the flow runs. Otherwise, you will get an error.

-Go to your authorization URL which was set in the previous section e.g http://localhost/authorize?resourceOwnerId=test

-If the resourceOwnerId field was not set then you can skip it in the URL and just use http://localhost/authorize

-The authorization and callback URL needs to be added to google drive beforehand for successful OAuth Dance

Once this is done, you will be able to see the successful acquisition of tokens in your flow.

Operation available for Google Drive Connector

-Create operations for files and folders

-Delete operations

-Modify operations

Adding/modifying metadata for file and folders

Here we are going to see two basic connector operations: Creating a simple media file and Creating a Multipart file (with metadata)

Creating Media file

From the list of the available connectors for google drive, drag the create media file connector

In the properties for the connector, choose the Google Drive connector config created at the start. To add data in the file populate the body field e.g payload in the body field. That's it!!!

A new untitled file will be created in your drive.

Creating Multipart File

Creating a Multipart file allows you to create a file with data along with adding metadata to it like name, extension, folder, user information, etc.

From the list of the available connectors for google drive, drag and drop the create multipart file connector.

In the connector, properties add the “Google Driver Config” file that was created at the start.

For multipart files two things need to be populated:

Fields-Body

Here mimeType represents the file extension.

Name defines file name

Parents define the folder id(you can find a google driver folder id in url of folder e.g https://drive.google.com/drive/folders/1MpQEvHp86u-Yzq9owbDgqd7i6RJ8R5Wp )

Content-Body

Content-Body contains the data that needs to be in the file. The data should already in the correct format as per requirement.

You can use MuleSoft Transform component to transform the data.

That's It!! A new file with the name DemoFile will be created in the specified folder.

References

--

--