Introduction To Google Drive API

As developers, we often face issues in maintaining, protecting and scaling the data of our server application. Maintaining our server’s storage gets tedious, but what if it can be done for you by some provider?

Launched in 2012, Google Drive is one of the most popular, revolutionary applications from Google; who could have imagined data sharing could be this simple? Google Drive is a cloud-based, user-friendly data storage service from Google, which helps in providing remote access to the files uploaded over its cloud system.

What made me utilize Google Drive API in my project?

There can be a lot of reasons for a developer to utilize a certain library, but here let me tell you mine. Due to some reason, the emails were not getting sent, and later we found out the attachments being sent were causing us trouble. So instead of sending the attachments directly with the mail, we replaced them with a google drive link uploaded on the drive.

Setting up Google Drive API

Getting started with Google Drive APIs is simple, and the official documentation is very helpful. So initial steps for setting up the library include:
1. Google Service account.
2. Primary user google drive account (You will know why to have a primary google drive account).

Google Service Account

A service account is a special account used by an application or computer workload, such as a Compute Engine virtual machine (VM) instance, rather than a person. Applications use service accounts to make authorized API calls, authorized as either the service account itself or as Google Workspace or Cloud Identity users through domain-wide delegation.

Layman Version

It’s a google account for your Library Instance. Any transaction with Google Drive will be done on the accounts of this service account from your library. Now let us set up the service account.

Creating a Google Service Account

Now that we know it is important to have a google service account to make our google drive API work, let’s create our google service account with the following steps:

Basic Setup: 

1. Login to Google Cloud Platform.

2. Create a project. 

3. Complete the necessary setup.

4. After completing all above steps here’s how the console would look.

Google Drive API l Google Cloud Platform.

And the Google Service account is ready to use. 

Enabling the Google Drive API 

It’s an important step to take care of; there are a lot of services that are provided by google, here we are enabling the ability to use google drive API for our google service account. Now let’s click on the APIs and Services link from the console and search for Drive APIs and Services > Search (Drive)

Google Drive API

And Enable the API, I’ve already enabled it so this is how it should look after you enable it.

Google Drive API

Problem

Well, not everything is perfect, and we have to deal with it; the same goes with the google service account(GSA).GSA doesn’t have any GUI, as provided by Google, so we won’t be able to see the files we are uploading. So how do we deal with this? As developers, we can query and see stuff in a drive, but how can a commoner query drive? In that case, we can use the concept of shared folders/drives.

Since the GUI is not provided to the GSA, but the user has the GUI for google drive, we can share a folder with that google service account and then we will be able to see the files easily.

Google Drive API

Implementing Code

Now that we are done with the setup let’s dive into the APIs and save a file into the shared folder.
But before we dive into the code, we need a JSON file that will authenticate us against the google services.
Console > IAM & Admin > Service Account

Google Drive API

Simply select the email > Key and download the json file.

Google Drive API

Now that we have our json file we can dive into the code and upload the file. 

Creating the service instance 

Using the code from official documentation

**

* Insert new file. 

* @return{obj} file Id 

* */ 

async function uploadBasic() { 

const fs = require('fs'); 

const {GoogleAuth} = require('google-auth-library'); 

const {google} = require('googleapis'); 

// Get credentials and build service 

// TODO (developer) - Use appropriate auth mechanism for your app 

const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive', keyFile:'./path_to_json_file'}); 

const service = google.drive({version: 'v3', auth}); 

const fileMetadata = { 

'title': 'photo.jpg', 

}; 

const media = { 

mimeType: 'image/jpeg', 

body: fs.createReadStream('files/photo.jpg'), 

}; 

try { 

const file = await service.files.create({ 

resource: fileMetadata, 

media: media, 

fields: 'id', 

}); 

console.log('File Id:', file.data.id); 

return file.data.id; 

} catch (err) { 

// TODO(developer) - Handle error 

throw err; 

} 


Here we have to make sure that while authenticating, we pass a keyFile property with the path of the JSON file we downloaded.

Add the parent’s property to the object to get the file in the shared folder.

Advantages

1. Share the data over google drive 

2. Add permission to the file 

3. Add User type permissions 

4. Add editor, viewer owner type permissions 

5. Send notification of the shared file

Disadvantages

1. The list goes on, and there are many other options available to select from. The APIs provided won’t let you down regarding the performance and quality of the data.

2. Moreover, if one of the issues we can have is the limited data size, so as long as you are within the limit, you won’t have to worry about the pricing and enjoy the services.

coma

Conclusion

Google Drive APIs are a real lifesaver for the server and developer regarding managing and securing the data. With almost zero response time, we can query our data with ease. These APIs can be helpful when it comes to storing and securing data with minimal setup.

Keep Reading

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?