我有一個將運行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
感謝您的幫助