nexus-1/api/gcalendar.py
2026-01-26 09:45:31 -05:00

51 lines
1.8 KiB
Python

import json
from google.oauth2 import service_account
from googleapiclient.discovery import build
def create_event(key, calendar_id, impersonator, summary, description, start, end, location, attendees, reminders):
"""
Creates a calendar event via Google Calendar API.
Arguments:
key: str : Service account credentials JSON string
calendar_id: str : Email address of the Google Calendar account
impersonator: str : Email address of the user to impersonate (domain-wide delegation)
summary: str : Event title
description: str : Event description
start: str : Start date/time in ISO format
end: str : End date/time in ISO format
location: str : Event location (address)
attendees: list : List of dicts with 'email' keys
reminders: list : List of dicts with 'method' and 'minutes' keys
Returns:
Created event object from Google Calendar API
"""
event = {
'summary': summary,
'description': description,
'start': {
'dateTime': start,
'timeZone': 'America/Detroit',
},
'end': {
'dateTime': end,
'timeZone': 'America/Detroit',
},
'location': location,
'attendees': attendees,
'reminders': {
'useDefault': False,
'overrides': reminders,
}
}
service_account_key = json.loads(key) if isinstance(key, str) else key
scopes = ['https://www.googleapis.com/auth/calendar']
credentials = service_account.Credentials.from_service_account_info(
service_account_key, scopes=scopes, subject=impersonator
)
service = build('calendar', 'v3', credentials=credentials)
return service.events().insert(calendarId=calendar_id, body=event).execute()