Integrating Firebase Cloud Functions with Google Calendar API
By: Scott Mccartney
Tags: Calendar, JavaScript, Google Cloud Platform, Firebase Cloud Functions, Firebase

Because we want our app to manage/edit/read from your Google Calendar, select the scope https://www.googleapis.com/auth/calendar for full read/write access. Once that scope is selected, click Authorize APIs at the bottom of Step 1.

You’ll be taken to a Google sign-in page where you sign in to the Google account who’s calendar you want to access. Once signed in, you’ll be asked to confirm access for your credentials to manage your Google calendars. Approve of this message to continue. This will redirect you to Step 2 of the OAuth2 Playground form.

OAuth2 Configuration Step 2

You should have the Authorization code populated. This code is only valid for a short period of time (3600 seconds precisely). While it’s active, you’ll need to click Exchange authorization code for tokens to populate a refresh token that we will be able to use later in our cloud function.

For this tutorial, we will add this refresh token to the credentials.json file we created earlier to limit the number of files accessed by our cloud function later. An updated version is below, and your file should be the same format.

Updated credentials.json File

There you go! You now have OAuth2 configured for your cloud function so that the Refresh token, Client ID, Client Secret can get you a valid Access token when working with the Google Calendar API. If you initialize this project as a Git repository, be sure to specify credentials.json in your .gitignore file so that you don’t accidentally add your credentials to a public repository.

Part 5: Write a Cloud Function to interact with Google Calendar

Now that we have OAuth2 configured for the Google Calendar API, we can begin writing code to interact with it! To do this, navigate to your project in the command line and go into the functions folder where your package.json lives. There, run npm i googleapis --save to install the necessary Google API Node.js tools.

First thing we’ll do is import all the required packages we’ll be using. The completed code for index.js can be found here. We’ll put this at the top of our file, before any methods we write later. Be sure that credentials.json is in the same folder as index.js , otherwise the path will be need to be updated.

Next, we’ll begin writing our Firebase function. In Part 2 of this tutorial, we wrote a helloWorld() Firebase function. We’ll replace that with the following addEventToCalendar() method.

The request and response parameters act as entry points and exits for our cloud function to interact with Firebase and the user. The request will have the data needed to make our calendar event, and we create that event in the first line of the function, storing it in eventData .

Next, we use our imported googleCredentials to create an oAuth2Client object with all the credentials we need to verify us as the correct user. We then set the refresh_token for the oAuth2Client credentials so that we have a fresh token when accessing the Google Calendar API.

Finally, we call a method addEvent() that has not been written yet, passing it the eventData and oAuth2Client details, waiting for either a Success or Failure. Once either is received, we pass the details of what happened back to the user with the response parameter given by Firebase.

The last method we need to write is the addEvent() method reference above. The full method is below:

As mentioned before, we passed our calendar event and authentication that were created in the initial addEventToCalendar() Firebase function. We then use the Google Calendar API by referencing the calendar object we created earlier. The full API for the Google Calendar can be found here. If successful, we pass the newly-created calendar event data back in the Promise with a message of Success that will be handled by the Firebase function we created above.

And that’s it! The Firebase function is now complete. Like before, we will deploy this function by running the command firebase deploy --only functions in the root directory of the project. After a minute or two, the Firebase CLI will say that the Cloud Function is deployed.

Part 6: Executing your Cloud Function

Once your function is deployed, go to the Google Developer Console and navigate to your Firebase project. In the menu on the left, select Cloud Functions from the list. There, you should see the Firebase function you just created in your Cloud Functions dashboard. Click the addEventToCalendar function to get more information about it.

Here, you should see four tabs for your cloud function: General, Trigger, Source, and Testing. Check out each one to learn more about your function, including its HTTP trigger endpoint.

We’ll start by going to the Testing tab though. Here, you’ll see the following input options:

Testing Dashboard for the Cloud Function

The empty {} JSON object is your request body. If you remember, we access several elements of this request body in our cloud function to create the calendar event. Because of this, we’ll need to replace the Triggering Event to something along the lines of:

{

"eventName": "Firebase Event",

"description": "This is a sample description",

"startTime": "2018-12-01T10:00:00",

"endTime": "2018-12-01T13:00:00"

}

Click Test the function to call your Firebase function with the above request body. If everything is correct, you should get a response with details about your newly created event. If you navigate to your Google Calendar, you’ll be able to see your event, exactly where it’s supposed to be.

Created Firebase Event

Conclusion

That’s it, we’ve officially got a Firebase Cloud Function deployed that will create events in your calendar with very minimal details needed in the request body. This function has an HTTP endpoint, so it can be hit by several different methods.

Further work could include using Alexa/Google Home to call that HTTP endpoint after adding specific voice prompts. Besides that, your interaction with the Calendar API could be extended by deleting events, modifying existing ones, reading out events occurring today, or others. Security for your Cloud Function could also be a next-step, so that you are sure that only you and your services are able to call your Cloud Function.

Now that you have a Cloud Function deployed in your Firebase project, I’d encourage you to look into several other Firebase services to expand your project even more. Good luck, and I hoped this help you learn something new about a cool, interesting technology!