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

Khushal Sahni
4 min readMar 26, 2022

--

Yes, Google APIs are complex af , To do the simplest of things there are tons of hoops one has to pass through to get stuff done. 😴

Therefore, Today I am going to show you a short and simple example of how to use the Google Calendar or any other Google APIs so you can use them easily in your app. 👌

Starting off here is the working code (Yes its a modified version of google’s documentation)

Procedure

  • 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! 😌

Understanding the code 👩‍💻

  • 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 calendar.events.insert 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 calendar.events.delete method, simple enough right? 😛

That’s it guys! Hope you were able to understand and will be able to use the powerful googleapis in your project. If you have any questions feel free to comment! Peace ✌️

--

--

Khushal Sahni
Khushal Sahni

Written by Khushal Sahni

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

No responses yet