谷歌日历api(红宝石)事件插入错误:缺少结束时间

时间:2013-11-17 02:49:16

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

我正在使用服务帐户并尝试将事件插入日历中。我正在使用ruby绑定。我不断收到错误

  

缺少结束时间

即使我发送了结束时间!

我的插入代码如下所示

event = {start: DateTime.now, end: (DateTime.now + 0.5)}
result = client.execute( api_method: calendar.events.insert, 
   parameters:{calendarId: 'MYCALENDARID@group.calendar.google.com'}, 
   authorization: client.authorization, 
   headers: { "Content-Type" => "application/json" }, 
   body: JSON.dump(event))

正如您所知,该活动有一个开始和结束日期!基于一些旧的SO答案,我也尝试过:

  • 将主体粘贴在一个数组中(如body: [JSON.dump(event)])导致异常(undefined method bytesize'为#`)
  • 使用body_object属性(如body_object: event),同样的问题

以供参考,设置如下:

require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'

client = Google::APIClient.new(:application_name => 'Example Ruby application',:application_version => '1.0.0')
key = Google::APIClient::KeyUtils.load_from_pkcs12('MYKEY.p12', 'notasecret')
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',:audience => 'https://accounts.google.com/o/oauth2/token',:scope => 'https://www.googleapis.com/auth/calendar',
issuer: 'MYSERVICEACCOUNT@developer.gserviceaccount.com', signing_key: key)
client.authorization.fetch_access_token!
calendar = client.discovered_api('calendar', 'v3')

1 个答案:

答案 0 :(得分:2)

docs(https://developers.google.com/google-apps/calendar/v3/reference/events/insert)给出了以下Ruby示例:

event = {
  'summary' => 'Appointment',
  'location' => 'Somewhere',
  'start' => {
    'dateTime' => '2011-06-03T10:00:00.000-07:00'
  },
  'end' => {
    'dateTime' => '2011-06-03T10:25:00.000-07:00'
  },
  'attendees' => [
    {
      'email' => 'attendeeEmail'
    },
    #...
  ]
}
result = client.execute(:api_method => service.events.insert,
                        :parameters => {'calendarId' => 'primary'},
                        :body => JSON.dump(event),
                        :headers => {'Content-Type' => 'application/json'})
print result.data.id

建议您确认输出开始/结束日期时间{start: DateTime.now, ...}的代码实际上是在创建您需要的嵌套{'start' => {'dateTime' => '2011-....'}, ...}

我不认识Ruby,但我的猜测是{'start' => {'dateTime' => DateTime.now}, ...}会做你需要的(对于开始和结束日期时间)。