根据文档 https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query 要执行 freebusy 查询,您必须在正文中提供以下参数:
{
"timeMin": datetime,
"timeMax": datetime,
"timeZone": string,
"groupExpansionMax": integer,
"calendarExpansionMax": integer,
"items": [
{
"id": string
}
]
}
构建查询时:
# calendarlist = a list with all the ids of the calendars I do query
# calendar is the access token to google calendar API
start = datetime.now()
end = start.replace(hour=23)
freebusy_query = {
"timeMin": start,
"timeMax": end,
"timeZone": 'Europe/Madrid',
"items": calendarlist
}
availability = calendar.freebusy().query(body=freebusy_query).execute()
我得到的回复告诉我:
TypeError: datetime.datetime(2017, 4, 14, 23, 00, 59, 999999)) is not JSON serializable
所以我想我对文档的阅读是错误的,我必须提供的是一个日期时间字符串
我说的对吗?
当我将日期时间转换为字符串时,如何保留 tzinfo?
date_format = '%Y-%m-%dT%H:%M:%S.%f%Z'
start_string = start.strftime(date_format)
...
试试这个
start = datetime.now().replace(microsecond=0).isoformat() + "-04:00"
end = datetime.now().replace(hour=23, microsecond=0).isoformat() + "-04:00"
freebusy = service.freebusy().query(body=
{"timeMin": start,
"timeMax": end,
"timeZone": 'Europe/Madrid',
"items": calendarList
}).execute()