How to Use the Google Drive API with JavaScript

Node.js, Authentication and Google’s Drive API

Bret Cameron

--

Google has a huge collection of APIs, and sooner-or-later in your development career, you’re likely to encounter one or more of them: from Google Maps to Google Calendar, YouTube to Google Analytics, and Gmail to Google Drive — there are APIs for a wide variety of tools in Google’s large product-base.

However, the scale of the documentation and the sheer number of available options can make it difficult for newer developers to get started with these tools. Plus, handling authentication can be tricky: there are multiple options depending on the tools you’re using and the context you’re using them in.

In this article, we’ll look into how to use the Google Drive API with JavaScript. If you want to code along to this article, make sure you have Node.js installed. Then, create a new directory for your project, navigate to it in the terminal and type npm init -y . Finally, create a file called index.js in the root folder of your new project.

Authentication

Before using any Google APIs, we’ll need to get past authentication. Visit console.developers.google.com and select a project or create a new one. Projects are used so that it’s easy to see any API activity and billing for a particular app or business in the same place, and you can access ‘credentials’ using the tab on the left.

(Unless you’re making a large amount of API calls, you won’t need to worry about billing. Google offers a generous amount for free, and this tutorial will keep you well within the limit!)

Option 1: API Key

This is the simplest option to authenticate, as it’s just a single string. But it’s also the least secure, which means that it isn’t enough for certain applications, and so we won’t worry about API keys in this article.

Option 2: OAuth 2.0

If you need users to provide permission to your app, you can use the OAuth 2.0 standard. This will ask users to authorize your app before you’ll be able to make changes using any APIs. This is a necessity for front-end applications and certain APIs (such as the Gmail API). But for our needs — a…

--

--

Bret Cameron

Writer and developer based in London. On Medium, I write about JavaScript and web development 💻