view calendar_connection.py @ 43:b5d3d9a8c83d

new graph of just the events happening now
author drewp@bigasterisk.com
date Mon, 19 Feb 2024 13:53:46 -0800
parents 2da773e48a57
children
line wrap: on
line source

import os
import pickle

from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build, Resource


def getCalendarService() -> Resource:
    """
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('credentials/token.pickle'):
        with open('credentials/token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    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/cred.json', [
                'https://www.googleapis.com/auth/calendar.events',
                "https://www.googleapis.com/auth/calendar.readonly",
            ])
            creds = flow.run_local_server()
        # Save the credentials for the next run
        with open('credentials/token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    return build('calendar', 'v3', credentials=creds)