Mercurial > code > home > repos > gcalendarwatch
view calendar_connection.py @ 28:e2209226b001
rewrite with starlette and background_loop
author | drewp@bigasterisk.com |
---|---|
date | Sun, 24 Jul 2022 00:58:54 -0700 |
parents | a87969972d85 |
children | 0d4b481f5bd3 |
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 SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'] def getCalendarService(scope='https://www.googleapis.com/auth/calendar.readonly') -> 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', [scope]) 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)