我正在尝试插入谷歌日历事件,如此处所示。如果我发送带有硬编码值的请求,例如示例,它可以正常工作:
event = {'summary' => 'Summary',
'location' => 'Location',
'start' => { 'dateTime' => '2011-06-03T10:00:00.000-07:00' },
'end' => { 'dateTime' => '2011-06-03T10:25:00.000-07:00' }}
但是如果我尝试发送变量,它会默默地失败:
event = {'summary' => "#{params[:summary]}",
'location' => "#{params[:location]}",
'start' => { 'dateTime' => "#{params[:start_time]}" },
'end' => { 'dateTime' => "#{params[:start_time]}" }}
如果我以这种方式格式化它也会失败:
event = "{
\"summary\": \"#{params[:summary]}\",
\"location\": \"#{params[:location]}\",
\"start\": { \"dateTime\": \"#{params[:start_time]}\" },
\"end\": { \"dateTime\": \"#{params[:end_time]}\" }
}"
我可以让它成功发布,start_time
格式end_time
如下:Date.today.strftime("%Y-%m-%dT%l:%M:%S.000-07:00")
,但这不太理想,因为我相信start_time
andend_time
参数已经是Time
对象。
表单正在传递参数,因为我在 Rails 控制台中得到了这个(authenticity_token
为简洁起见省略):
Started POST "/events" for 127.0.0.1 at 2015-04-15 08:53:36 -0600
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"...",
"event"=>{"summary"=>"Summary",
"start_time(1i)"=>"2015",
"start_time(2i)"=>"4",
"start_time(3i)"=>"15",
"start_time(4i)"=>"14",
"start_time(5i)"=>"54",
"end_time(1i)"=>"2015",
"end_time(2i)"=>"4",
"end_time(3i)"=>"15",
"end_time(4i)"=>"14",
"end_time(5i)"=>"55",
"location"=>"Location"},
"commit"=>"Create Event"}
以下是相关代码:
client = Google::APIClient.new({:application_name => "PlannerApp",
:application_version => "1.0"})
client.authorization.access_token = t.fresh_token
service = client.discovered_api('calendar', 'v3')
client.execute(:api_method => service.events.insert,
:parameters => {'calendarId' => 'primary'},
:body => JSON.dump(event),
:headers => {'Content-Type' => 'application/json'})
这是我从谷歌得到的(Authorization
为了简洁而省略):
Google::APIClient::Request Sending API request get https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest {
"User-Agent"=>"PlannerApp/1.0 google-api-ruby-client/0.8.5 Mac OS X/10.10.2\n (gzip)",
"Accept-Encoding"=>"gzip",
"Content-Type"=>""}
Google::APIClient::Request Result: 200 {
"expires"=>"Wed, 15 Apr 2015 14:58:37 GMT",
"date"=>"Wed, 15 Apr 2015 14:53:37 GMT",
"etag"=>"\"ye6orv2F-1npMW3u9suM3a7C5Bo/snCOExYvhrzEZTGzdubZtafuHVU\"",
"vary"=>"Origin, X-Origin",
"content-type"=>"application/json; charset=UTF-8",
"x-content-type-options"=>"nosniff",
"x-frame-options"=>"SAMEORIGIN",
"x-xss-protection"=>"1; mode=block",
"content-length"=>"93847",
"server"=>"GSE",
"cache-control"=>"public, max-age=300, must-revalidate, no-transform",
"age"=>"0",
"alternate-protocol"=>"443:quic,p=0.5",
"connection"=>"close"}
Google::APIClient::Request Sending API request post https://www.googleapis.com/calendar/v3/calendars/primary/events {
"User-Agent"=>"PlannerApp/1.0 google-api-ruby-client/0.8.5 Mac OS X/10.10.2\n (gzip)",
"Content-Type"=>"application/json",
"Accept-Encoding"=>"gzip",
"Authorization"=>"Bearer ...",
"Cache-Control"=>"no-store"}
我应该如何格式化这些event
哈希值以获得成功POST
?
解决方案:
event = {'summary' => "#{params[:event][:summary]}",
'location' => "#{params[:event][:location]}",
'start' => { 'dateTime' => Time.new(params['event']['start_time(1i)'],
params['event']['start_time(2i)'],
params['event']['start_time(3i)'],
params['event']['start_time(4i)'],
params['event']['start_time(5i)'])
.to_datetime.rfc3339,
'timeZone' => "America/Denver" },
'end' => { 'dateTime' => Time.new(params['event']['end_time(1i)'],
params['event']['end_time(2i)'],
params['event']['end_time(3i)'],
params['event']['end_time(4i)'],
params['event']['end_time(5i)'])
.to_datetime.rfc3339,
'timeZone' => "America/Denver" }}