Google 日历 API 使用服务帐户时出现 HttpError 404

Google Calendar API HttpError 404 when using service account

我正在尝试从服务帐户获取我日历上的事件列表,这样我就不必对自己进行身份验证即可显示我的日历。当我 运行 以下代码出现 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 是 google 日历 Web 应用程序左下方的列表。

除非您使用 clanedarlist.insert 将日历插入到服务帐户日历列表中,否则它不会显示在该列表中。

与服务帐户共享日历后,只需对您共享的日历 ID 执行 calendar.get,您就可以访问它。如果你真的想要它在日历列表中,你可以做一个 calendarlist.insert。