Google Calendar V3 Ruby API插入事件

时间:2016-01-27 09:18:28

标签: ruby google-calendar-api google-api-ruby-client

我正在使用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)

然而,我收到了错误:

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'

示例中的事件对象有什么问题? 我试图使用dateTime时的dateTime,但错误是一样的。

UPD: 最初的例子:

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: 'lpage@example.com'},
    {email: 'sbrin@example.com'},
  ],
  reminders: {
    use_default: false,
    overrides: [
      {method' => 'email', 'minutes: 24 * 60},
      {method' => 'popup', 'minutes: 10},
    ],
  },
}

enter image description here 显然,有很多语法错误。 (错过')方法'=> 'popup','分钟(错过'):10

所以,我决定修改它:

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: 'lpage@example.com'},
    {email: 'sbrin@example.com'},
  ],
  reminders: {
    use_default: false,
    overrides: [
      {'method' => 'email', 'minutes'=> 24 * 60},
      {'method' => 'popup', 'minutes'=> 10},
    ],
  }
}

但是,我仍然得到http_command.rb:202:在`check_status'中:required:缺少结束时间。 (谷歌::蜜蜂:: ClientError)

4 个答案:

答案 0 :(得分:3)

在您的示例中,您创建的红宝石块等于:

Google::Apis::CalendarV3::Event.new do
  # something
end

但你想要的是传递这样的哈希:

Google::Apis::CalendarV3::Event.new({
  key: value
})

答案 1 :(得分:2)

对我来说,atm,使用这种语法:

Google::Apis::CalendarV3::Event.new({
  :summary => 'event title',
  :location => 'event address',
  :description => 'event description',
  :start => {
    :date_time => 'event start time in rfc3339'
  },
  :end => {
    :date_time => 'event end time in rfc 3339'
  }
})

答案 2 :(得分:1)

我解决了我的问题。事件哈希必须是这样的:

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'
  }
})

另外,请不要忘记通过来自json文件的服务电子邮件提供对日历的访问权限。 “client_email”:“bla@blablabla.iam.gserviceaccount.com”,

答案 3 :(得分:0)

在提醒作品中,:reminder_method

Link here

以下示例

{   
    :summary=>"Never underestimate yourself!",                                                     
    :location=>"In my brain",
    :description=>"Never underestimate\nwhat you can acomplish\nwhen you believe in yourself.\n\nNever give up.\n\nhttp://www.youtube.com/watch?v=qX9FSZJu448",
    :attendees => [                                                                                
        {:email =>'Sample.Mail@gmail.com'},                                                        
    ],                                                                                             
    :start=>{                                                                                      
        :date_time=>"2019-05-12T11:30:00+02:00",                                                   
        :time_zone=>"Europe/Warsaw"                                                                
    },                                                                                             
    :end=>{                                                                                        
        :date_time=>"2019-05-12T11:30:15+02:00",                                                   
        :time_zone=>"Europe/Warsaw"
    },                                                              
    :reminders=>{                                                                                  
        :use_default=>false,                                                                       
        :overrides=>[                                                                              
            {:reminder_method=>"email", :minutes=>35}                                              
            {:reminder_method=>"popup", :minutes=>5}                                               
        ]                                                                                          
    }                                                                                              
}