0

So I've got this Ruby on rails app going, I have it set up with a service account to do server to server requests for the google calendar API. I have got the calendar object which has methods that include insert_event.

class CalendarController < ApplicationController
require 'googleauth'
require 'google/apis/calendar_v3'
  def create_event
    calendar = Google::Apis::CalendarV3::CalendarService.new
    scopes =  ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/drive']
    calendar.authorization = Google::Auth.get_application_default(scopes)
    token = calendar.authorization.fetch_access_token!

    event = {
      'summary' => 'Google I/O 2015',
      'location' => '800 Howard St., San Francisco, CA 94103',
      'description' => 'A chance to hear more about Google\'s developer products.',
      'start' => {
        'dateTime' => '2015-05-28T09:00:00-07:00',
        'timeZone' => 'America/Los_Angeles',
      },
      'end' => {
        'dateTime' => '2015-05-28T17:00:00-07:00',
        'timeZone' => 'America/Los_Angeles',
      },
      'recurrence' => [
        'RRULE:FREQ=DAILY;COUNT=2'
      ],
      'attendees' => [
        {'email' => 'myemail1@gmail.com'},
        {'email' => 'jdong8@gmail.com'},
      ],
      'reminders' => {
        'useDefault' => false,
        'overrides' => [
          {'method' => 'email', 'minutes' => 24 * 60},
          {'method' => 'popup', 'minutes' => 10},
        ],
      },
    }
    calendar.insert_event(event, 'primary')
  end
end

When I try running calendar.insert_event(event, 'primary') I get this 404 error

404 (165 bytes) 338ms>
{"domain"=>"global", "reason"=>"notFound", "message"=>"Not Found"}
Caught error {"domain"=>"global", "reason"=>"notFound", "message"=>"Not Found"}
Error - #<Google::Apis::ClientError: {"domain"=>"global", "reason"=>"notFound", "message"=>"Not Found"}>
Google::Apis::ClientError: {"domain"=>"global", "reason"=>"notFound", "message"=>"Not Found"}

The main documentation for the google calendar API uses a different set up around a client object that doesn't match with the service account documentation which suggest making a calendar object. Does anyone know how to do this even if it is a very different implementation Ideally though I would like to know what the ? I want to be able to put stuff on my calendar whenever a customer makes a delivery request.

1 Answers1

3

Turns out the problem was mostly with the event, for this set up it has to be a special google object rather than a hash. Here's the code to make it work, I found it in gems/google-api-client-0.9.pre1/samples/calendar/calendar.rb

require 'googleauth'
require 'google/apis/calendar_v3'

calendar = Google::Apis::CalendarV3::CalendarService.new
scopes =  ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/drive']
calendar.authorization = Google::Auth.get_application_default(scopes)
token = calendar.authorization.fetch_access_token!
emails = ["me@example.com","myboss@example.com"]
# Create an event, adding any emails listed in the command line as attendees
event = Calendar::Event.new(summary: 'A sample event',
                            location: '1600 Amphitheatre Parkway, Mountain View, CA 94045',
                            attendees:  emails.each { |email| Calendar::EventAttendee.new(email: email) },
                            start: Calendar::EventDateTime.new(date_time: DateTime.parse('2015-12-31T20:00:00')),
                            end: Calendar::EventDateTime.new(date_time: DateTime.parse('2016-01-01T02:00:00')))
event = calendar.insert_event('primary', event, send_notifications: true)
puts "Created event '#{event.summary}' (#{event.id})"
end
  • Just in case this is for google-api-ruby-client v.0.9 (current Alpha), and the example is available here: https://github.com/google/google-api-ruby-client/blob/master/samples/calendar/calendar.rb – Hugo Dec 16 '15 at 12:19
  • @graytmatter you saved me a lot of time. After playing with the API explorer, I end up where you started. For me, I had to use Google::Apis::CalendarV3::EventDateTime and Google::Apis::CalendarV3::Event but I wouldn't have found that easily. The parameters to insert_event are also confusing -- note the first two are a string and an object, the others are name: value. You can get that if you are careful about the documentation at http://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/CalendarV3/CalendarService#insert_event-instance_method but I wasn't – jpa57 Feb 10 '17 at 22:09