问题描述
我正在尝试从服务帐户获取日历中的事件列表,以便不必验证自己的身份即可显示日历。当我运行以下代码时,出现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日历网络应用程序左下方的列表。
除非您使用clanedarlist.insert将日历插入到服务帐户日历列表中,否则日历不会显示在该列表中。
与服务帐户共享日历后,只需在您共享的日历ID上做一个calendar.get,便可以访问它。然后,如果您确实需要在日历列表中,可以执行一个calendarlist.insert。