Using Python backend in Google Functions to edit a personal Google Calendar

Hey guys, I’m trying to change my personal Google Calendar using Python in Google Functions. I’ve got some code working that adds an event, and the Google console shows it’s doing something. But I’m not sure which calendar it’s actually changing.

When I put in my personal calendar ID, it throws an error. I thought the service account had all the right permissions. Can I mess with any calendar in my organization using the service account?

Here’s my code:

from google.oauth2 import service_account
from googleapiclient.discovery import build
from datetime import datetime, timedelta

SERVICE_ACCOUNT = 'my_account@example.iam.gserviceaccount.com'

def update_calendar():
    creds = service_account.Credentials.from_service_account_file('service_file.json', scopes=['https://www.googleapis.com/auth/calendar'])
    delegated_creds = creds.with_subject(SERVICE_ACCOUNT)
    
    cal_service = build('calendar', 'v3', credentials=delegated_creds)

    today = datetime.utcnow().date()
    next_day = datetime(today.year, today.month, today.day + 1, 9)
    start_time = next_day.isoformat()
    end_time = (next_day + timedelta(hours=1)).isoformat()
    
    event_details = {
        "summary": "Automated Calendar Test",
        "description": "Testing Python calendar automation",
        "start": {"dateTime": start_time, "timeZone": 'America/New_York'},
        "end": {"dateTime": end_time, "timeZone": 'America/New_York'}
    }

    cal_service.events().insert(calendarId=SERVICE_ACCOUNT, body=event_details).execute()

if __name__ == "__main__":
    update_calendar()

It seems to work, but where can I see which calendar I’m actually changing? Any help would be awesome!

It seems you’re using the service account email as the calendar ID, which is incorrect. To fix this, you need to use your actual calendar ID. You can find this in your Google Calendar settings—it’s usually in the format ‘username@gmail.com’ or a long string of characters. Also, make sure your service account has the necessary permissions to access and modify your personal calendar by adding the service account email as a user with edit rights. Lastly, remove the line delegated_creds = creds.with_subject(SERVICE_ACCOUNT) as it’s not needed when using a service account directly. Replace SERVICE_ACCOUNT in the insert() method with your actual calendar ID. After making these changes, your code should work correctly and you’ll be able to see the new events in your personal Google Calendar interface.

hey there! have you checked if your service account has permission to access your personal calendar? it might be trying to add events to the wrong one. maybe try granting it access explicitly in your calendar settings? also, double-check your calendar id - it should look like ‘your.email@gmail.com’ or a long string. let us know if that helps!

hey, looks like you’re using your service account email as calendar id. try using your actual calendar id (for instance, a string like ‘yourname@gmail.com’ or similar) and ensure your service account is granted access to that calendar. hope it works!