我有一个将运行cron操作的python脚本。 它需要查找当天的事件列表,然后根据事件执行一些其他操作。 到目前为止,我可以向Calendar API发出请求,并获取当天事件列表。 当我循环显示事件项目列表时,该项目的“摘要”键缺失。 我需要这个字段,以便我可以确定事件的目的。Python Google Calendar API v3在列出事件项时不会返回“摘要”字段
在每个事件项目对应的数据是回来了类似这样的没有“摘要”键
{
"status": "confirmed",
"kind": "calendar#event",
"end": {
"date": "2017-07-29"
},
"iCalUID": "[email protected]",
"updated": "2017-06-20T00:00:00.000Z",
"start": {
"date": "2017-07-24"
},
"etag": "\"0000000000000000\"",
"id": "0000000000000"
}
在这里找到https://developers.google.com/google-apps/calendar/v3/reference/events#resource它显示了“摘要”键应该与该事件被返回的谷歌文档。
由于此脚本将自动运行,因此我设置了一个Google Service帐户来授予对API的请求,以便用户不必手动对其进行授权。下面是脚本的样本,我使用
# -*- coding: utf-8 -*-
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient.discovery import build
import datetime
try:
scopes = ['https://www.googleapis.com/auth/calendar']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'/path/to/credentials/filename.json', scopes=scopes)
http_auth = credentials.authorize(Http())
startTime = str(datetime.datetime.now().date()) + 'T00:00:00-07:00'
endTime = str(datetime.datetime.now().date()) + 'T23:59:00-07:00'
calendar = build('calendar', 'v3', http=http_auth)
calId = 'calendar_id_string'
response = calendar.events().list(calendarId=calId,
timeMin=startTime, timeMax=endTime).execute()
items = response.get('items',[])
for item in items:
summary = item.get('summary','') # summary is blank!!
except Exception, ex:
print ex
感谢您的帮助