Skip to content

Instantly share code, notes, and snippets.

@TheStaticTurtle
Created February 8, 2022 23:44
Show Gist options
  • Save TheStaticTurtle/24a7af899d4b01d5f6d3ab8239cc928e to your computer and use it in GitHub Desktop.
Save TheStaticTurtle/24a7af899d4b01d5f6d3ab8239cc928e to your computer and use it in GitHub Desktop.
Google calendar service
import datetime
import config
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar']
def get_calendar_service():
creds = None
if os.path.exists("gcal_token.pickle"):
with open("gcal_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("gcal_client_secret.googleusercontent.com.json", SCOPES)
creds = flow.run_local_server(port=0)
with open("gcal_token.pickle", 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
return service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment