从特定日历获取事件 Google API Python

Getting events from specific calendar Google API Python

我使用 Google API 中的 quickstart.py 代码从日历中获取事件。它是下面的代码,确切的步骤在这里 https://developers.google.com/calendar/quickstart/python

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import datetime

# Setup the Calendar API
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('calendar', 'v3', http=creds.authorize(Http()))

# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
print('Getting the upcoming 10 events')
events_result = service.events().list(calendarId='primary', timeMin=now,
                                  maxResults=10, singleEvents=True,
                                  orderBy='startTime').execute()
events = events_result.get('items', [])

if not events:
    print('No upcoming events found.')
for event in events:
    start = event['start'].get('dateTime', event['start'].get('date'))
    print(start, event['summary'])

这对我来说很好用。现在我在我的帐户中添加了另一个日历,将我的代码复制到另一个文件夹中并再次 运行 通过步骤 (google link)。我把secound .json放到了secound文件夹下测试了.

即使它应该能够访问两个日历,它也只能从第一个日历中检索事件。所以我想知道如何访问另一个日历(最好只有第二个日历,但我同时使用它们也会有帮助)。

我确实搜索了 Google 文档,但没有帮助,我发现的每个 Whosebug 问题都依赖于另一种编程语言(我没有将其附加到 python)或答案包含 links 到不再存在的网站。

非常感谢!

您需要 select 日历,您希望从中检索将正确的 calendarId 传递给 service.events().list(). 'primary' can always be used to get the primary calendar of the user, you can get the information of all the calendars the user has with service.calendarList().list() 的事件。