I have searched the web for the solution but it seemed I am the only one who couldn't solve this issue.
So this is what my python code looks like:
class ReminderDate(object):
def __init__(self, datetimestring, timezone="Australia/Sydney"):
self.dateTime = datetimestring
self.timeZone = timezone
class ReminderFormat(object):
def __init__(self, useDefault=False):
self.useDefault = useDefault
self.overrides = [{"method":"email", "minutes":15}]
class ReminderData(object):
def __init__(self, reminder, datefrom=None, dateto=None, datevalue=None):
self.summary = reminder
self.start = ReminderDate(datefrom)
self.end = ReminderDate(dateto)
self.reminders = ReminderFormat()
def save_event_to_google_calendar(self, reminder_data):
credentials = self.get_credentials()
service = build('calendar', 'v3', http=credentials.authorize(Http()))
event = json.dumps(reminder_data, default=lambda o: o.__dict__)
print (event)
created_event = service.events().insert(calendarId=CALENDAR_ID, body=str(event), sendNotifications=True).execute()
pp.pprint(created_event)
And it produces hence the print(event) outputs a json like below:
{"start": {"timeZone": "Australia/Sydney",
"dateTime": "2015-04-26T18:45:00+10:00"},
"end": {"timeZone": "Australia/Sydney",
"dateTime": "2015-04-26T19:00:00.000+10:00"},
"reminders": {"overrides": [{"minutes": 15, "method": "email"}],
"useDefault": false},
"summary": "do grocery shopping"}
and I get the following error:
File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 729, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError:
<HttpError 400 when requesting https://www.googleapis.com/calendar/v3/calendars/myemail%40gmail.com/events?alt=json&sendNotifications=true
returned "Missing end time.">
I dont understand. I have the "end" time in the json. Then what am I missing here ?
I have tried posting the same json via the google developer console https://developers.google.com/google-apps/calendar/v3/reference/events/insert and it works. BUT it fails from the python client library :( (described above)
[EDIT] Solution found
The issue was that I had converted Python Object to JSON "string" and that's what I was supplying as opposed to JSON "object" expected by the API. So this is the right code:
json_event = json.loads(event)
created_event = service.events().insert(calendarId=CALENDAR_ID, body=json_event, sendNotifications=True).execute()