3

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()
JMax
  • 26,109
  • 12
  • 69
  • 88
Ali Nahid
  • 867
  • 11
  • 26
  • Are you sure you should pass a `timeZone`? There is no such value in API example. – awesoon Apr 25 '15 at 03:41
  • I have tried both (with and without timeZone).. the result was the same. Besides even though the sample doesn't show it's in the API as an optional property. – Ali Nahid Apr 25 '15 at 12:40
  • solved issue for me also passing a dict into the param instead of json. – Zaffer Feb 20 '23 at 22:20

1 Answers1

3

Try adding:

httplib2.debuglevel = 4

just before the API call. This will allow you to see the exact body that the events.insert() request is sending. Compare that to the body produced by API explorer and you should have your answer.

Jay Lee
  • 13,415
  • 3
  • 28
  • 59
  • @JeyLee Thank you so much ( and very clever way to point me to the right direction ). So basically the issue was ( in case any one is wondering ) 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 was the solution: `json_event = json.loads(event) created_event = service.events().insert(calendarId=CALENDAR_ID, body=json_event, sendNotifications=True).execute()` – Ali Nahid Apr 27 '15 at 04:49
  • This was a great help to me. In my case, I was reusing a variable that I was using to build my request, and I could see the incorrect value in the request using this solution – Grezzo Nov 13 '16 at 19:07