How to use Google Calendar APIs with NodeJs with Oauth 2.0 🎉

  • Clone the above given repository, do an npm i in the repo
  • Go to Google Developer console, create a new project, enable the Google Calendar API through the Enabled APIs and Services tab
  • In the Oauth Consent Screen tab you will have to fill in a few details such as the scopes (For now you can add all Google Calendar API scopes or you can choose as you want)
  • Go to credentials tab and create credentials for Oauth Client, When prompted for Application type choose Web Application.
  • In the Authorized Redirect URIs add http://localhost:8000/auth/google/callback (This is just a route we are using in the above repo, you can use anything you want)
  • Once you have created the credentials download them (They’ll be in a json format) copy it over to the repo (place it on the same level as google.js) and rename them to credentials.json
  • That’s it! Now in google.js file you can change the event object as and how you want, change the yourEmail variable value to your email and now you can run it simply by node google.js , Viola! 😌
  • Here we define the scopes we need for our app, you can pass an array of all the scopes your application needs, also make sure you have added those scopes in your project in Google Developer Console
  • Here fs module reads credentials.json file which you downloaded, and then parses it using JSON.parse and passes it to the authorize method
  • In the authorize method we deconstruct the client_secret and other details from credentials.web using Object destructuring
  • Initializes the oAuth2Client by passing values to Oauth2 method of google.auth
  • Now it sees if we already have a token, if not we will call the getAccessToken method or else we set credentials for oauth client using our token
  • This method calls generateAuthUrl with the required details and generates an authUrl clicking which you will be redirected to the oauth login screen where you will have to login with the email you specified above
  • Once you login, your meeting will be scheduled
  • This is the route which is hit when we login with our account, google sends a code in the query parameter of the redirect url, we capture that code and get the token for that code
  • We also save the token for future use, so you don’t have to login all the time
  • In the last line scheduleMeeting is called
  • In this method we create an instance of google calendar with auth, and then call the method
  • As you can see it returns lots of data including things like the html link and the event id which you can store and use in other methods as well
  • On that note there is a bonus function included for further understanding
  • This method requires an event id and calls the method, simple enough right? 😛




SDE-1 @ Urban Company, Let’s help each other out :)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Implementing the async/await syntax for promise request JavaScript

How can we convert an H5 app into a Quick App with Online Conversion

The Circle of Strife

The JavaScript Prototypal Inheritance Pattern

Static Factory Method: An Alternative to Constructor Overloading in JavaScript

Event Loop Video Blogamajigger

Building Small Form-Based JS Apps: Step-by-step with ES6

Should You Be Using Meal Plans to Lose Weight? (find out here)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Khushal Sahni

Khushal Sahni

SDE-1 @ Urban Company, Let’s help each other out :)

More from Medium

Building a Standard API

Models from JSON API response

GraphQL, what, why, and when?

Microservice from Scratch (3): Write the microservice template