python - 使用服务帐户时的 Google Calendar API HttpError 404
问题描述
我正在尝试从服务帐户获取日历上的事件列表,这样我就不必验证自己来显示我的日历。当我运行以下代码时,出现 404 错误:
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
SERVICE_ACCOUNT_FILE = os.path.join(os.path.dirname(
os.path.realpath(__file__)), 'static/calendarSync/service-account.json')
calId = "blakewright1021@gmail.com"
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
google_account = googleapiclient.discovery.build(
'calendar', 'v3', credentials=credentials)
cal_list = google_account.calendarList() # pylint: disable=no-member
#page_token = None
#calendar_list = cal_list.list(pageToken=page_token).execute()
# for calendar_list_entry in calendar_list['items']:
# print("Here: ")
# print(calendar_list_entry['summary'])
calendar = cal_list.get(
calendarId=calId)
output = calendar.execute()['summary']
当我使用注释掉的代码尝试打印日历列表时,它不会打印任何内容,因此列表必须为空。
这是回溯:
Internal Server Error: /
Traceback (most recent call last):
File "C:\Python38\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Python38\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python38\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\blake\djangoCalendar\locallibrary\calendarSync\views.py", line 40, in index
output = calendar.execute()['summary']
File "C:\Python38\lib\site-packages\googleapiclient\_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Python38\lib\site-packages\googleapiclient\http.py", line 907, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/calendar/v3/users/me/calendarList/blakewright1021%40gmail.com?alt=json returned "Not Found">
我已与服务帐户共享我的日历。当我在没有服务帐户的情况下进行身份验证时,我也可以访问日历。我究竟做错了什么?
解决方案
CalendarList 是谷歌日历网络应用程序左下角的列表。
除非您已使用clanedarlist.insert 将日历插入服务帐户日历列表,否则它不会显示在该列表中。
与服务帐户共享日历后,只需在您共享的日历 ID 上执行 calendar.get 即可访问它。然后,如果您真的想要日历列表中的内容,您可以执行 calendarlist.insert。