2

我已经在网上搜索了解决方案,但似乎我是唯一无法解决此问题的人。

所以这就是我的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 中有“结束”时间。那我在这里想念什么?

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


[编辑] 找到解决方案

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

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

1 回答 1

3

尝试添加:

httplib2.debuglevel = 4

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

于 2015-04-25T14:23:38.213 回答