Search code examples
google-apifetchgoogle-calendar-api

No "colorId" field returned in the Event object with the Google calendar api v3


I'm trying to just query a list of my calendar events for any given calendar using the v3 calendar api, an api key, and the browser fetch (or postman). I created a test calendar "Test Calendar 2", made it public, made an event, added a color to it, and made an api key on the google console.

Using this get url: https://www.googleapis.com/calendar/v3/calendars/CALENDAR_ID/events?key=MY_API_KEY, I get back a list of events like so:

{
    "kind": "calendar#events",
    "etag": "\"_\"",
    "summary": "Test Calendar 2",
    "description": "",
    "updated": "2024-04-06T04:47:30.237Z",
    "timeZone": "America/Los_Angeles",
    "accessRole": "reader",
    "defaultReminders": [],
    "nextSyncToken": "_==",
    "items": [
        {
            "kind": "calendar#event",
            "etag": "\"_\"",
            "id": "_",
            "status": "confirmed",
            "htmlLink": "https://www.google.com/calendar/event?eid=_",
            "created": "2024-04-05T04:12:13.000Z",
            "updated": "2024-04-06T03:34:03.072Z",
            "summary": "Hello",
            "description": "Description",
            "creator": {
                "email": "_@gmail.com"
            },
            "organizer": {
                "email": "_@group.calendar.google.com",
                "displayName": "Test Calendar 2",
                "self": true
            },
            "start": {
                "dateTime": "2024-04-01T19:00:00-07:00",
                "timeZone": "America/Los_Angeles"
            },
            "end": {
                "dateTime": "2024-04-01T20:00:00-07:00",
                "timeZone": "America/Los_Angeles"
            },
            "visibility": "public",
            "iCalUID": "_@google.com",
            "sequence": 0,
            "eventType": "default"
        }
    ]
}

However a bunch of properties from the Event object are missing (most importantly colorId). As described here: https://developers.google.com/calendar/api/v3/reference/events#resource

What am i missing? Is this field somehow restricted or can i get this to work?

Reading lots of mixed info about the colorId field online. I read that the colorId field may be undefined if the event only has its default color. But i updated it multiple times and it doesn't seem to return anything.


Solution

  • The problem is that colorId is now a private property of a Google Calendar event. To access this field, you need to follow these steps.

    1. Create OAuth

    • Go to the Google Calendar API section and click Create Credentials (OAuth Client ID) in the Credentials section.
    • In the Scopes section, add .../auth/calendar.events.
    • In the Application Type section, select Desktop App.
    • Download your JSON file with credentials.

    2. Perform Authentication (Python)

    • Create a new Python project and install the dependencies with:
    pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
    
    • Add your downloaded JSON file to the root of the project and name it credentials.json.
    • Add following code to main.py file
    
       from google.oauth2.credentials import Credentials
       from google_auth_oauthlib.flow import InstalledAppFlow
       from google.auth.transport.requests import Request
       import os
       import json
       
       # The required OAuth2 scopes for accessing Google Calendar
       SCOPES = ['https://www.googleapis.com/auth/calendar']
       
       
       def get_credentials():
           creds = None
           # The file where the access and refresh tokens are stored after the first run
           token_file = 'token.json'
       
           if os.path.exists(token_file):
               creds = Credentials.from_authorized_user_file(token_file, SCOPES)
           # If there are no valid credentials, initiate the OAuth flow
           if not creds or not creds.valid:
               if creds and creds.expired and creds.refresh_token:
                   creds.refresh(Request())
               else:
                   flow = InstalledAppFlow.from_client_secrets_file(
                       'credentials.json', SCOPES)  # Use the credentials.json file that contains your client_id and secret
                   creds = flow.run_local_server(port=0)
               # Save the credentials for the next run
               with open(token_file, 'w') as token:
                   token.write(creds.to_json())
       
           return creds
       
       
       if __name__ == '__main__':
           get_credentials()
           print("OAuth flow completed, token saved to 'token.json'")
    
    
    • Run the script:
    python main.py
    
    • You will get a token.json file.

    3. Use the Token

    • Instead of using key={api_key} in your Google Calendar API requests, use the token from the token.json file.
    • The token should be passed in the request header as:
    Authorization: Bearer {token}
    

    Additional:

    To refresh the token, use the following request (the data should be taken from token.json):

        POST https://oauth2.googleapis.com/token
        Content-Type: application/x-www-form-urlencoded
        
        client_id={your_client_id}&
        client_secret={your_client_secret}&
        refresh_token={your_refresh_token}&
        grant_type=refresh_token