我正在使用Google Calendar的API来获取当天的活动。发生的奇怪事情是当我使用calendarId='primary'
列表方法时,不会返回任何结果。但是,如果我输入日历的电子邮件,它可以正常工作。有什么理由呢?
代码:
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient import discovery
from apiclient.discovery import build
import os
import datetime
from datetime import timedelta
def main():
scopes = ['https://www.googleapis.com/auth/calendar.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name('/path/to/keyfile.json', scopes=scopes)
http_auth = credentials.authorize(Http())
service = discovery.build('calendar', 'v3', http=http_auth)
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
later = datetime.datetime.utcnow() + timedelta(days=20)
later = later.isoformat() + 'Z'
print("Getting events for the day...")
eventsResult = service.events().list(calendarId='primary', timeMin=now, timeMax=later, singleEvents=True, orderBy='startTime').execute()
events = eventsResult.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'])
if __name__ == '__main__':
main()