Skip to content

Commit f194140

Browse files
Update script.py
Updated authentication code to a more redundant format.
1 parent ce10fff commit f194140

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

Google-Meet-Scheduler/script.py

+31-25
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
from googleapiclient.discovery import build
22
from uuid import uuid4
33
from google.auth.transport.requests import Request
4-
from pathlib import Path
54
from google_auth_oauthlib.flow import InstalledAppFlow
65
from typing import Dict, List
7-
from pickle import load, dump
6+
import os
7+
from google.oauth2.credentials import Credentials
8+
9+
SCOPES = ["https://www.googleapis.com/auth/calendar"]
810

911

1012
class CreateMeet:
11-
def __init__(self, attendees: Dict[str, str], event_time: Dict[str, str], topic):
13+
def __init__(self, attendees: Dict[str, str],
14+
event_time: Dict[str, str], topic):
1215
authe = self._auth()
13-
attendees = [{"email": e} for e in attendees.values()]
16+
attendees_list = [{"email": e} for e in attendees.values()]
1417
self.event_states = self._create_event(
15-
attendees, event_time, authe, topic)
18+
attendees_list, event_time, authe, topic)
1619

1720
@staticmethod
18-
def _create_event(attendees: List[Dict[str, str]], event_time, authe: build, topic):
21+
def _create_event(
22+
attendees: List[Dict[str, str]], event_time, authe: build, Topic):
1923
event = {"conferenceData": {"createRequest": {"requestId": f"{uuid4().hex}", "conferenceSolutionKey": {"type": "hangoutsMeet"}}},
2024
"attendees": attendees,
2125
"start": {"dateTime": event_time["start"], 'timeZone': 'Asia/Kolkata'},
2226
"end": {"dateTime": event_time["end"], 'timeZone': 'Asia/Kolkata'},
23-
"summary": topic,
27+
"summary": Topic,
2428
"reminders": {"useDefault": True}
2529
}
2630
event = authe.events().insert(calendarId="primary", sendNotifications=True,
@@ -29,23 +33,24 @@ def _create_event(attendees: List[Dict[str, str]], event_time, authe: build, top
2933

3034
@staticmethod
3135
def _auth():
32-
token_file, scopes = Path(
33-
"./token.pickle"), ["https://www.googleapis.com/auth/calendar"]
34-
credentials = None
35-
if token_file.exists():
36-
with open(token_file, "rb") as token:
37-
credentials = load(token)
38-
if not credentials or not credentials.valid:
39-
if credentials and credentials.expired and credentials.refresh_token:
40-
credentials.refresh(Request())
36+
creds = None
37+
if os.path.exists("token.json"):
38+
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
39+
# If there are no (valid) credentials available, let the user log in.
40+
if not creds or not creds.valid:
41+
if creds and creds.expired and creds.refresh_token:
42+
creds.refresh(Request())
4143
else:
4244
flow = InstalledAppFlow.from_client_secrets_file(
43-
'credentials.json', scopes)
44-
credentials = flow.run_local_server(port=0)
45-
with open(token_file, "wb") as token:
46-
dump(credentials, token)
47-
calendar_service = build("calendar", "v3", credentials=credentials)
48-
return calendar_service
45+
"credentials.json", SCOPES
46+
)
47+
creds = flow.run_local_server(port=0)
48+
# Save the credentials for the next run
49+
with open("token.json", "w") as token:
50+
token.write(creds.to_json())
51+
52+
service = build("calendar", "v3", credentials=creds)
53+
return service
4954

5055

5156
print('------------------------------')
@@ -60,9 +65,10 @@ def _auth():
6065
emails = list(
6166
input('Enter the emails of guests separated by 1 space each : ').strip().split())
6267
topic = input('Enter the topic of the meeting : ')
68+
6369
time = {
64-
'start': date+'T'+start+':00.000000',
65-
'end': date+'T'+end+':00.000000'
70+
'start': date + 'T' + start + ':00.000000',
71+
'end': date + 'T' + end + ':00.000000'
6672
}
6773
guests = {email: email for email in emails}
6874
meet = CreateMeet(guests, time, topic)
@@ -72,4 +78,4 @@ def _auth():
7278
print('-- Meeting Details --')
7379
print('---------------------')
7480
for key in keys:
75-
print(key+' : ', details[key])
81+
print(key + ' : ', details[key])

0 commit comments

Comments
 (0)