Search code examples
rubygoogle-calendar-apigoogle-api-ruby-client

Google Calendar V3 Ruby API insert event


I am using example from https://developers.google.com/google-apps/calendar/v3/reference/events/insert

event = Google::Apis::CalendarV3::Event.new{
  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: {
    date_time: '2015-05-28T09:00:00-07:00',
    time_zone:'America/Los_Angeles',
  },
  end: {
    date_time:'2015-05-28T17:00:00-07:00',
    time_zone:'America/Los_Angeles',
  }
}

result = service.insert_event(calendar_id, event)

However, I got error:

wwtlf:~/workspace $ ruby test.rb 
/usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.9.1/lib/google/apis/core/http_command.rb:202:in `check_status': required: Missing end time. (Google::Apis::ClientError)
        from /usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.9.1/lib/google/apis/core/api_command.rb:103:in `check_status'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.9.1/lib/google/apis/core/http_command.rb:170:in `process_response'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.9.1/lib/google/apis/core/http_command.rb:275:in `execute_once'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.9.1/lib/google/apis/core/http_command.rb:107:in `block (2 levels) in execute'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/retriable-2.1.0/lib/retriable.rb:54:in `block in retriable'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/retriable-2.1.0/lib/retriable.rb:48:in `times'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/retriable-2.1.0/lib/retriable.rb:48:in `retriable'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.9.1/lib/google/apis/core/http_command.rb:104:in `block in execute'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/retriable-2.1.0/lib/retriable.rb:54:in `block in retriable'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/retriable-2.1.0/lib/retriable.rb:48:in `times'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/retriable-2.1.0/lib/retriable.rb:48:in `retriable'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.9.1/lib/google/apis/core/http_command.rb:96:in `execute'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.9.1/lib/google/apis/core/base_service.rb:267:in `execute_or_queue_command'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.9.1/generated/google/apis/calendar_v3/service.rb:1207:in `insert_event'

What is wrong with event object from example? I tried to use dateTime insted of date_time, but error is the same.

UPD: The original example:

event = Google::Apis::CalendarV3::Event.new{
  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: {
    date_time: '2015-05-28T09:00:00-07:00',
    time_zone: 'America/Los_Angeles',
  },
  end: {
    date_time: '2015-05-28T17:00:00-07:00',
    time_zone: 'America/Los_Angeles',
  },
  recurrence: [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  attendees: [
    {email: '[email protected]'},
    {email: '[email protected]'},
  ],
  reminders: {
    use_default: false,
    overrides: [
      {method' => 'email', 'minutes: 24 * 60},
      {method' => 'popup', 'minutes: 10},
    ],
  },
}

enter image description here Obviously, there are a lot of syntax errors. (missed ')method' => 'popup', 'minutes(missed '): 10

So, I decided to modify it:

event = Google::Apis::CalendarV3::Event.new{
  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: {
    date_time:'2015-05-28T09:00:00-07:00',
    time_zone:'America/Los_Angeles',
  },
  end: {
    date_time:'2015-05-28T17:00:00-07:00',
    time_zone:'America/Los_Angeles',
  },
  recurrence: [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  attendees: [
    {email: '[email protected]'},
    {email: '[email protected]'},
  ],
  reminders: {
    use_default: false,
    overrides: [
      {'method' => 'email', 'minutes'=> 24 * 60},
      {'method' => 'popup', 'minutes'=> 10},
    ],
  }
}

But, I still got http_command.rb:202:in `check_status': required: Missing end time. (Google::Apis::ClientError)


Solution

  • I solved my issue. Event hash must be like this:

    event = Google::Apis::CalendarV3::Event.new({
      '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':{
        'date_time': DateTime.parse('2016-05-28T09:00:00-07:00'),
        'time_zone': 'America/Los_Angeles'
      },
      'end':{
        'date_time': DateTime.parse('2016-05-28T17:00:00-07:00'),
        'time_zone': 'America/Los_Angeles'
      }
    })
    

    Also don't forget to provide the access to your calendar by severice accaunt email from json file. "client_email": "[email protected]",