google calendar api (ruby) event insert error: Missing end time
-
21-12-2019 - |
题
I'm using a service account and attempting to insert an event into a calendar. I'm using the ruby bindings. I constantly get an error
Missing end time
even though i am sending an end time!
my insert code looks like this
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))
as you can tell, the event has a start and end date! based on some old SO answers i also tried:
- sticking the body in an array (like
body: [JSON.dump(event)]
) which resulted in an exception (undefined method
bytesize' for #`) - using a body_object property instead (like
body_object: event
), same issue
and for reference, the setup looks like:
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')
解决方案
The docs (https://developers.google.com/google-apps/calendar/v3/reference/events/insert) give the following Ruby example:
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
Suggest you confirm that your code that outputs the start/end date times {start: DateTime.now, ...}
is actually creating the nested {'start' => {'dateTime' => '2011-....'}, ...}
that you need.
I don't know Ruby, but my guess is that {'start' => {'dateTime' => DateTime.now}, ...}
will do what you need (for both start and end datetimes) .