Skip to content

Instantly share code, notes, and snippets.

Created September 27, 2020 12:13
What would you like to do?
Gets the tasks for today using the google calendar api
def get_tasks_from_calendar():
# source:
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
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 modifying these scopes, delete the file token.pickle.
SCOPES = ['']
if os.path.exists('token.pickle'):
with open('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:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
print('Getting the upcoming 10 events')
events_result ='primary', timeMin=now,
maxResults=10, singleEvents=True,
tasks = events_result.get('items', [])
task_list = []
if not tasks:
print('No upcoming tasks found.')
for event in tasks:
today = str(
task_date = event['start'].get('dateTime', event['start'].get('date'))
if today in task_date:
return task_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment