Access >=1 Google Calendar via the API with service account credentials

Can anyone point me to clear, up-to-date documentation on how to access >=1 Google Calendar with service account credentials?

Most of the Calendar API docs describe using a user's Google account credentials and not a service account. 

After a lot of digging, I was able to cobble together the following:

```
import os
from google.oauth2 import service_account
from googleapiclient.discovery import build

# Path to your service account key file
SERVICE_ACCOUNT_FILE = os.environ["GOOGLE_APPLICATION_CREDENTIALS"]

# Define the scopes
SCOPES = ['https://www.googleapis.com/auth/calendar']

# Authenticate and construct service
credentials = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('calendar', 'v3', credentials=credentials)

# List available calendards
calendar_list = service.calendarList().list().execute()

# Iterate through calendars and print their details
for calendar in calendar_list.get('items', []):
    print(f"Calendar ID: {calendar['id']}, Calendar Summary: {calendar['summary']}")
```

However, no calendars are printed (`calendar_list.items = []`).

The IT at my organization say that they have added the calendars the service account, but they couldn't really find clear documentation on this task, and had to just guess, to some extent. 

Likely, this is a permissions issue, but how am I supposed to determine what the issue is?

I've tried adding a calendar via the python SDK:
```
service.calendarList().insert(body=calendar_list_entry).execute()
```
as described at https://developers.google.com/calendar/api/v3/reference/calendarList/insert#python, but that results in the following error:
```
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/calendar/v3/users/me/calendarList?alt=json returned "Not Found". Details: "[{'domain': 'global', 'reason': 'notFound', 'message': 'Not Found'}]">
```

Note: the python example at https://developers.google.com/calendar/api/v3/reference/calendarList/insert#python is still Python v2.

0 2 729
2 REPLIES 2

Hello @nick-youngblut ,

Here is a 3rd-party documentation from Jackie

  1. In the calendar settings allow the service access to your account...
 
 
  1. Once added you can access the calendar using the email instead of primary...
  def getCalendarSummary = {
    val credentials: GoogleCredentials = GoogleCredentials
      .fromStream(new FileInputStream(keyLocation))
      .createScoped(Collections.singleton(CalendarScopes.CALENDAR_EVENTS))
    credentials.refreshIfExpired()
    val HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport
    new Calendar.Builder(
      HTTP_TRANSPORT,
      JSON_FACTORY,
      new HttpCredentialsAdapter(credentials)
    )
      .setApplicationName(appName)
      .build()
      .calendars()
      .get("person@gmail.com")
      .execute()
      .getSummary
  }

The service account can now add and remove events

@dionv Thank you for the suggestion. The "third party documentation" that you refer to is just a Stack Overflow post. Also, the solution if for java, and not python. Can you please point me to up-to-date Google docs on an python-based solution?