Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 11, 2022 02:02 am GMT

CRUD with Google Calendar API & Python

Install dependencies

requirements.txt

google-api-python-client google-auth-httplib2 google-auth-oauthlib
pip install -r requirements.txt

get_service.py

from os import pathimport picklefrom google.auth.transport.requests import Requestfrom google_auth_oauthlib.flow import InstalledAppFlowfrom googleapiclient.discovery import build# IF YOU MODIFY THE SCOPE DELETE THE TOKEN.TXT FILESCOPES = ['https://www.googleapis.com/auth/calendar.events',          'https://www.googleapis.com/auth/calendar']# THE TOKEN.TXT FILE STORES THE USER'S UPGRADE AND ACCESS TOKENS# creds.json -> Credentials in Oauth0def get_crendetials_google():    # OPEN THE BROWSER TO AUTHORIZE    flow = InstalledAppFlow.from_client_secrets_file("creds.json", SCOPES)    creds = flow.run_local_server(port=0)    # SAVE CREDENTIALS    pickle.dump(creds, open("token.txt", "wb"))    return credsdef get_calendar_service():    creds = None    if path.exists("token.txt"):        creds = pickle.load(open("token.txt", "rb"))    # IF IT EXPIRED WE REFRESH THE CREDENTIALS    if not creds or not creds.valid:        if creds and creds.expired and creds.refresh_token:            creds.refresh(Request())        else:            creds = get_crendetials_google()    service = build("calendar", "v3", credentials=creds)    return service

Template JSON

{  "summary": "Important event!",  "location": "virtual event (Slack)",  "description": "this is a description",  "start": {    "dateTime": "2021-12-10T10:00:00",    "timeZone": "America/El_Salvador"  },  "end": {    "dateTime": "2021-12-10T11:00:00",    "timeZone": "America/El_Salvador"  },  "attendees": [{ "email": "[email protected]" }],  "reminders": {    "useDefault": false,    "overrides": [      { "method": "email", "minutes": 30 },      { "method": "popup", "minutes": 10 }    ]  }}

CRUD Google Calendar

calendar.py

from functions.get_service import get_calendar_servicefrom responses.response_json import response_jsonservice = get_calendar_service()def create_event(template: dict):    try:        response = service.events().insert(calendarId="primary", body=template).execute()        return response    except Exception as e:        return e.messagedef get_event(eventId: str):    try:        response = service.events().get(calendarId="primary", eventId=eventId).execute()        return response    except Exception as e:        return e.messagedef delete_event(eventId: str):    try:        response = service.events().delete(calendarId="primary", eventId=eventId).execute()        return response    except Exception as e:        return e.messagedef update_event(eventId: str, template: dict):    try:        response = service.events().update(calendarId='primary',                                           eventId=eventId, body=template).execute()        return response    except Exception as e:        return e.message

Original Link: https://dev.to/nelsoncode/crud-with-google-calendar-api-python-3jbc

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To