Getting Started with Google Drive Node.js API

Google Drive API is a Google product developed to enable developers to create applications that use Google Drive cloud storage. Through the API, you can develop an application that seamlessly integrates Google Drive for data storage and sharing.

1. Introduction

Among the functionalities provided by the API are uploading, deleting, sharing, and downloading files that exist in a Google Drive account.

2. Goal

By the end of this tutorial, we’ll have built a basic Node.js application that uploads, deletes, and generate a sharable link of a file uploaded in Google Drive. We will use the API to carry out all these activities.

This tutorial mainly focuses on the backend integration of the API.

3. Project setup

Create a directory named drive-api-node by executing the command below.

mkdir drive-api-node
cd drive-api-node

To create the Node.js application, we will run the command below to initialize a package.json file.

npm init -y

Next, we will install the googleapis dependency by the following command.

npm install –save googleapis

Create a file named index.js in the root folder. We will write most of our code in this file. This file is the entry point of the application.

4. Setting up Auth credentials

We need a client ID and secret to access the drive storage.

  1. Visit the console.cloud.google.com.
  2. Create a new project if you do not have an existing project.
  3. Select the project and select APIs and services.
  4. Click on ENABLE APIS AND SERVICES
  5. In the search box, search for google drive api then enable it for the project created.
  6. Once you have enabled the API, select CREATE CREDENTIALS for accessing the API. In the drop-down menu, select Oath client ID. This option will generate a key pair of client ID and client secret.
  7. Next, select CONFIGURE CONSENT SCREEN which will ask the user if they want the application to access their Drive using the created client ID and secret. Select on External then click the CREATE button.
  8. Enter the appname for your application, the support email. I ignored the app logo for now because it will request verification. Provide the requested details then click Save to return to the Credentials screen.
  9. Select Web Application for the Application Type. Follow the instructions to enter JavaScript origins, redirect URLs I used this for the redirect URL. Click Create.
  10. We can now copy the client ID and client secret to our application.
  11. On the Auth Comscent Screen, click ADD USER then insert a valid email whose Google account will be used for testing the application.

5. Import the dependencies

Add the code snippets below to include the installed dependencies to the project. The file system and path modules will enable us to get the path of the file to be uploaded to Google drive.

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

//path module
const path = require('path');

//file system module
const fs = require('fs');

6. Client ID and secret

We can obtain the CLIENT_ID and SECRECT from the dashboard as we had created them earlier. In the index.js, add the snippets below to include the client ID and secret.

//client id
const CLIENT_ID = 'YOUR CLIENT IS'

//client secret
const CLIENT_SECRET = 'YOUR CLIENT SECRET';

7. Redirect URL and refresh token

Use the oathplayground link as the redirect_url.

To generate the refresh token,

  1. Head over to oathplayground and select Drive Api v3.
  2. Under the Drive Api v3, select this link https://www.googleapis.com/auth/drive to enable the access, edit, creation, and deletion of Google Drive files.
  3. On the top right, click the setting icon then in the drop down menu, check the use your own Auth credentials checkbox.
  4. In the input boxes, fill in the client ID and secret then click close then Authorize Apis.
  5. In the next step, we will select the Google account for which we will use with the Drive. Use the account which you added as a test user in the Auth Consent Screen.
  6. In the next screen, click allow to grant the API access to Google Drive for editing, creation, and deletion of files.
  7. You will be redirected back to the oathplayground. Click exchange authorization code for tokens to get the refresh token.

Now store the refresh token in constant as shown below.

//redirect URL
const REDIRECT_URI = 'https://developers.google.com/oauthplayground';

//refresh token
const REFRESH_TOKEN = 'YOUR REFRESH TOKEN`

8. The cuth client

To initialize the auth client, add the snippets below in the index.js file.

//intialize auth client
const oauth2Client = new google.auth.OAuth2(
    CLIENT_ID,
    CLIENT_SECRET,
    REDIRECT_URI
);

Next, we need to set auth credentials in the initialized auth client. To set the auth credentials, add the snippets below to the initialized auth client.

//setting our auth credentials
oauth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });

9. Initialize Google Drive

To initialize the Google Drive, add the snippets below.

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

Next, we specify the path to the file for which we want to upload to Google drive.

//file path for out file
const filePath = path.join(__dirname, 'filename.format');

10. The Google Drive functions

We have set up everything for the project. Now we need to add the code snippets for the basic functionalities as specified in the project goal.

We will write three functions to upload a file to the Google drive, delete a specified file, and generate a shareable link of a given file existing in the Google Drive.

10.1. Uploading a file to Google Drive

The uploadfile function taken no parameter. It is an asynchronous function that sends a request and waits for a response from the server. In the request body we supply the filename and mimeType.

The mimeType specifies the file format. You can find more about mimeType here.

The snippets below shows the function to upload a file to Google drive:

//function to upload the file
async function uploadFile() {
    try{
      const response = await drive.files.create({
            requestBody: {
                name: 'hero.png', //file name
                mimeType: 'image/png',
            },
            media: {
                mimeType: 'image/png',
                body: fs.createReadStream(filePath),
            },
        });  
        // report the response from the request
        console.log(response.data);
    }catch (error) {
        //report the error message
        console.log(error.message);
    }
}  

10.2. The delete file function

Each file in the Google Drive has a unique ID. The delete file function will delete the file specified by the ID in the request body. The snippets below are added to the index.js file to delete a file.

//delete file function
async function deleteFile() {
    try {
        const response = await drive.files.delete({
            fileId: 'File_id',// file id
        });
        console.log(response.data, response.status);
    } catch (error) {
        console.log(error.message);
    }
  }

For the file ID, when the Upload file() function is called, the function returns the file id of the uploaded file in the response. During testing, be sure to copy the file ID in the response. Data for use in the deleteFile() function.

This function is responsible for creating a link to access the file in the drive folder. First, the function should change the permission to public for anyone to access. This process is necessary because during upload, only the user who uploaded the file has access to the file.

//create a public url
async function generatePublicUrl() {
    try {
        const fileId = '19VpEOo3DUJJgB0Hzj58E6aZAg10MOgmv';
        //change file permisions to public.
        await drive.permissions.create({
            fileId: fileId,
            requestBody: {
            role: 'reader',
            type: 'anyone',
            },
        });

        //obtain the webview and webcontent links
        const result = await drive.files.get({
            fileId: fileId,
            fields: 'webViewLink, webContentLink',
        });
      console.log(result.data);
    } catch (error) {
      console.log(error.message);
    }
  }

The webContent link downloads the file to the system directly. The webViewLink is a link for viewing the file in a browser without any credentials.

11. Conclusion

In this tutorial, I explained how to set up Google Drive with Node.js. We wrote functions to upload, delete, and share a file in Google Drive.

You can go ahead and try out the application on your computer by the source code here:

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

const CLIENT_ID = 'YOUR CLIENT_ID'
const CLIENT_SECRET = 'YOUR CLIENT_SECRET';
const REDIRECT_URI = 'YOUR REDIRECT_URI';

//refresh token
const REFRESH_TOKEN = YOUR REFRESH_TOKEN';

//intialize auth client
const oauth2Client = new google.auth.OAuth2(
    CLIENT_ID,
    CLIENT_SECRET,
    REDIRECT_URI
);

//setting outr credentials
oauth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });

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

//file path for out file
const filePath = path.join(__dirname, 'filename');

//function to upload the file
async function uploadFile() {
    try{
      const response = await drive.files.create({
        requestBody: {
            name: 'filename', //file name
            mimeType: 'image/png',
        },
        media: {
            mimeType: 'image/png',
            body: fs.createReadStream(filePath),
        },
      });
  
        console.log(response.data);
    } catch (error) {
        console.log(error.message);
    }
}
  
uploadFile()
//delete file function
async function deleteFile() {
    try {
        const response = await drive.files.delete({
            fileId: 'file id',
        });
        console.log(response.data, response.status);
    } catch (error) {
        console.log(error.message);
    }
  }


//create a public url
async function generatePublicUrl() {
    try {
        const fileId = 'file id';
        //change file permisions
        await drive.permissions.create({
            fileId: fileId,
            requestBody: {
            role: 'reader',
            type: 'anyone',
            },
        });

        const result = await drive.files.get({
            fileId: fileId,
            fields: 'webViewLink, webContentLink',
        });
      console.log(result.data);
    } catch (error) {
      console.log(error.message);
    }
  }

  generatePublicUrl()
hero.png

Happy coding!

Related posts:

Debugging a Node.js app running in Docker using Nodemon and the Docker extension
How To Develop An Interactive Command Line Application Using Node.js
Writing A Multiplayer Text Adventure Engine In Node.js: Adding Chat Into Our Game (Part 4)
Getting Started with Node.js Paypal Checkout Integration
Node.js Structural Comparisons
Getting Started with billboard.js charts
How to use Streams in Node.js
How To Secure Your Web App With HTTP Headers
APIs in Node.js vs Python - A Comparison
Making a Discord Bot (With Node.js)
Hapi vs Koa vs Express
How to Send SMS in Node.js using Vonage's SMS API
How to build a real time chat application in Node.js
The Nodemailer package in a Node.js Server
Building A Video Streaming App With Nuxt.js, Node And Express
How To Build A Skin For Your Web App With React And WordPress
Getting Started with Node.js Module
An Introduction To Node.js And MongoDB
Creating A Continuous Integration Test Workflow Using GitHub Actions
Get Started With Node: An Introduction To APIs, HTTP And ES6+ JavaScript
Optimizing Critical-Path Performance With Express Server And Handlebars
Writing A Multiplayer Text Adventure Engine In Node.js (Part 1)
How to use CORS in Node.js with Express
Getting Started With Node.js Timers
Better Error Handling In NodeJS With Error Classes
Documenting a Node.js REST API using Swagger
Building your First Telegram Bot using Node.js and Telegraf
Creating Secure Password Resets With JSON Web Tokens
Open-sourced node.js modules at Browserling
The Registration API becomes RESTful
How to Get SSL HTTPS for Localhost
How To Build A Node.js API For Ethereum Blockchain