Getting Started with the Google API for Reading Public Calendars
I have a few public calendars at work that we use to show events taking place in various departments. I am creating a digital sign for one of our buildings and I’d like to be able to grab the events for the current day and display them on the digital sign. I’m most familiar with ruby right now, so I’m writing a ruby script to do this.
The first thing is I need to install the ruby google-api-client.
$ gem install google-api-client
Since all of the calendar data that I want to grab is on public calendars, I can use an API key to get all this data. Instructions for generating an API key are here: https://support.google.com/cloud/answer/6158862. You also need the calendarid for the calendars you want to grab data from.
In order to see if I have things set up correctly, the following script will grab all the entries from the calendar specified by the calendarid.
require 'google/apis/calendar_v3' Google::Apis.logger.level = Logger::DEBUG calendar = Google::Apis::CalendarV3::CalendarService.new calendar.key='YOUR_API_KEY' puts calendar.list_events('GOOGLE_CALENDAR_ID') $ ruby simple_test.rb ...all calendar events show...
With the key working correctly, how can I limit the results to just a single day? And only see the fields that I’m interested in? For us, these are the summary, description, start date_time and location.
require 'google/apis/calendar_v3' calendar = Google::Apis::CalendarV3::CalendarService.new calendar.key='YOUR_API_KEY' events = calendar.list_events('GOOGLE_CALENDAR_ID', always_include_email: false, time_min: '2018-04-23T00:00:00-05:00', time_max: '2018-04-23T23:59:59-05:00' ) events.items.each do |item| puts item.summary puts item.description puts item.start.date_time puts item.location puts "=====" end
I hope to have this script do a bit more. So I’ve put it on my github page. The repository is here: https://github.com/maryheintz/google-calendar-api-ruby