-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.py
More file actions
92 lines (80 loc) · 2.88 KB
/
Copy pathquickstart.py
File metadata and controls
92 lines (80 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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"]
def main():
"""Shows basic usage of the Google Calendar API.
Adds an event to user's calenadar then
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)
# Call the Calendar API
now = datetime.datetime.now(tz=datetime.timezone.utc).isoformat()
# Creates Event Object to add to Calendar
event = {
'summary': 'ES@P TEST EVENT',
'location': 'Purdue Bell Tower, Central Dr, West Lafayette, IN 47907',
'description': 'A chance to hear more about Google\'s developer products.',
'start': {
'dateTime': '2025-11-28T09:00:00-04:00',
'timeZone': 'America/New_York',
},
'end': {
'dateTime': '2025-11-28T17:00:00-04:00',
'timeZone': 'America/New_York',
},
'recurrence': [
'RRULE:FREQ=DAILY;INTERVAL=2;COUNT=2'
]
}
# Adds Event Object to Calendar
event = service.events().insert(calendarId='primary', body=event).execute()
print ('Event created: %s' % (event.get('htmlLink')))
print("Getting the upcoming 10 events")
events_result = (
service.events()
.list(
calendarId="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()