Python Google 日历 API v3 插入事件抛出 "Missing end time" 错误的客户端库

Python client library for Google Calendar API v3 insert event throwing "Missing end time" error

我在网上搜索了解决方案,但似乎只有我一个人无法解决这个问题。

这就是我的 python 代码的样子:

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)

因此它会产生 print(event) 输出 json,如下所示:

{"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"}

我收到以下错误:

    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.">

我不明白。我在 json 中有 "end" 时间。那我在这里错过了什么?

我已经尝试通过 google 开发人员控制台 https://developers.google.com/google-apps/calendar/v3/reference/events/insert 发布相同的 json 并且它有效。 但是它从 python 客户端库失败:((如上所述)


[编辑] 找到解决方案

问题是我已经将 Python 对象转换为 JSON "string",这就是我提供的,而不是 JSON "object" 期望的API。 所以这是正确的代码:

json_event = json.loads(event)
created_event = service.events().insert(calendarId=CALENDAR_ID, body=json_event, sendNotifications=True).execute()

尝试添加:

httplib2.debuglevel = 4

就在 API 调用之前。这将允许您查看 events.insert() 请求发送的确切主体。将其与 API explorer 生成的主体进行比较,您应该得到答案。