Google Calendar API HttpError 404 when using service account

Viewed 632

I am trying to get a list of events on my calendar from a service account so that I do not have to authenticate myself to display my calendar. When I run the following code I get a 404 error:

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']

When I use the code that is commented out to try and print a list of calendars, it does not print anything so the list must be empty.

Here is the traceback:

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">

I have shared my calendar with the service account. I am also able to access the calendar when I authenticate without a service account. What am I doing wrong?

1 Answers

CalendarList is the list on the bottom left of the google calendar web application.

Unless you have inserted the calendar into the service accounts calendarlist using clanedarlist.insert, its not going to show up in that list.

Once you have shared a calendar with the service account just do a calendar.get on the calendar id you shared and you will be able to access it. You can then do a calendarlist.insert if you really want it in the calendar list.

Related