Files
OpenDisplay/gcal_quickstart.py
2026-05-27 01:34:43 +00:00

93 lines
3.5 KiB
Python

import datetime
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"]
def main():
"""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.json 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("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
# 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.json", SCOPES
)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
try:
service = build("calendar", "v3", credentials=creds)
calendarids = []
calendarsummaries = []
calendar_list = (service.calendarList().list()).execute().get("items", [])
for result in calendar_list:
calendarids.append(result.get("id",[]))
calendarsummaries.append(result.get("summary",[]))
print("result",result.get("summary",[]),result.get("id",[]))
print("summaries",calendarsummaries)
print("first result",calendar_list[0])
print("first result",calendar_list[len(calendar_list)-1])
#result meneelyl@gmail.com meneelyl@gmail.com
#result Family family14345103599446093558@group.calendar.google.com
#result Meneely Birthdays fjlvq3dnhfvn76ihljocbrhrdc@group.calendar.google.com
#result Tim Meneely2 k7iohvqfspej540e37bqr1hkso@group.calendar.google.com
#result meneelyt@gmail.com meneelyt@gmail.com
#result 1532 Ingomar Heights Road 3a0104e2f97668033b30227726b79f48b9f341a84cd9fa57aa4e23851aee1750@group.calendar.google.com
#result Vivienne fb53f1ba30abe33a395cb3483aaba416675e2ac829e9e74ca25c10d4fae593c5@group.calendar.google.com
#result Benjamin ed47cdac4f626432af8c0d1956499d258d0365cf3fe6ee70ce73d7d478b0f1dd@group.calendar.google.com
#result Madeline c6eed2e9ec829d835357032a344d2f6894677a6e652d04345c73249cd9bff8fb@group.calendar.google.com
# Call the Calendar API
now = datetime.datetime.now(tz=datetime.timezone.utc).isoformat()
print("Getting the upcoming 10 events")
events_result = (
service.events()
.list(
calendarId="family14345103599446093558@group.calendar.google.com", #"meneelyt@gmail.com", #"ed47cdac4f626432af8c0d1956499d258d0365cf3fe6ee70ce73d7d478b0f1dd@group.calendar.google.com", #"primary",
timeMin=now,
maxResults=10,
singleEvents=True,
orderBy="startTime",
)
.execute()
)
events = events_result.get("items", [])
if not events:
print("No upcoming events found.")
return
# Prints the start and name of the next 10 events
for event in events:
start = event["start"].get("dateTime", event["start"].get("date"))
print(start, "--", event["summary"])
except HttpError as error:
print(f"An error occurred: {error}")
if __name__ == "__main__":
main()