Mercurial > code > home > repos > gcalendarwatch
view calendar_connection.py @ 91:62caecb41dfd default tip
fix tag
author | drewp@bigasterisk.com |
---|---|
date | Fri, 03 Jan 2025 18:06:13 -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)