Google calendar is a very robust application to create a calendar event. It allows users to add events, schedule meets, and track the timeline of any project.

Integrating Google calendar in Ruby on Rails development allows users to access the calendar from within the app and add or remove events. It is a useful app for tracking progress of activities inside an application.

Ruby on Rails Development: Google Calendar API Integration

There is an API to manage whole google calendar features programmatically. But it is challenging to work with the APIs as the API document is not comprehensive. There is no proper documentation for each piece in any library or SDK we use.

In one of the Ruby on Rails projects, we needed to set up two-way communication between the Ruby on Rails application and the user’s google calendar.

  1. When a user creates an event in the Rails application, we need to push that event to Google Calendar via API. We have written a comprehensive blog for this.
  2. When a user creates a Google Calendar event, we need to save that record in our database. We can achieve this in an async manner using the notification channel which Google Calendar provides.

What is the Notification Channel?

It is a channel through which the user will be notified through Google Calendar API for any of the changes in the watched resource i.e Calendars in Google Calendar API.

There are some prerequisites to create and configure notification channels.

1. Domain Registration in search console

Note:  To use this in the Ruby on Rails development environment, you simply need to use the ngrok URL for domain registration in developers and search console. If the host is being used anywhere in the Ruby on Rails application, the host value must be modified as well.

  • Click on Add Property to add your domain in the menu on the left-hand side.
add property
  • Fill in the form to get your application’s domain verified.select property - ruby on rails calendar
  • Click on continue and download the HTML file provided and place it in the public folder to get it verified.
  • The domain/URL will be verified using the downloaded HTML file.

2. Domain Registration in the developer’s console.

  • Register domain in the developer’s console.
  • Add your web-app domain to the developer’s console for your project in the Google APIs console in the Domain Verification Section.
  • Click on Add Domain and add the domain of your web app.

3. Create a notification channel to receive changes in the web app.

  • Request Parameters:
    • Id: Unique identifier for a channel so use UUID as recommended by GOOGLE CALENDAR API
    • type: set to web_hook
    • address: webhook callback URL to receive notifications.
    • callback_url: POST request to receive notifications in the web app.
    • token(optional): arbitrary string value used as channel token or any additional parameter to use for getting notifications for a particular user or event.
  • Response Parameters:
    • Id: Channel ID specified in the request.
    • resource_id: Unique identifier for the resource to be watched.
    • resource_uri: Version-specific ID of the watched resource.
    • token: Present only if provided in the request.

Note:  If there is a requirement to push google events for a particular user then we need to pass that user’s id(unique identifier) in the token which can be received in every notification callback as X-Goog-Channel-Token in request headers.

4. Make a POST route for the watch event of the resource(Google Calendar) in your web app and receive notifications.

  • Make controller and action for the POST route specified to get notified in the web app.

Note:

  1. Changes will be received in the web app in this POST route and request headers will contain channel_id, resource_id, channel_token.
  2. The domain must be registered to the search console and developers console to use Google Calendar API. Also, the domain should be SSL verified.

ALL SET!!

You will now receive notifications in your web-app for the event created in the google calendar and customize as per your requirement.

Additionally, if you want to revoke the Google Calendar access from the user, you should first stop the channel to receive the notifications and then revoke access.

You can find the whole source code for 2 way communication for Google Calendar API here.

Conclusion

There are different alternatives to integrating the Google Calendar API with the Ruby on Rails application. But if you have followed the above structure correctly, you will be able to integrate it without any hassle.

After the integration, you will be able to list events, list calendar, add new events and add guests to the scheduled meeting – all with the application. It removes the task of going separately to Google calendar and then adding the tasks.

Read through the blog how to create events in Google Calendar API in rails application reference for more details.

Consulting is free – let us help you grow!