Skip to content

Commit 2b06d4a

Browse files
authored
Adjust try/catch block
1 parent b2427f1 commit 2b06d4a

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

calendar/quickstart/quickstart.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from google.oauth2.credentials import Credentials
2323
from google_auth_oauthlib.flow import InstalledAppFlow
2424
from googleapiclient.discovery import build
25+
from googleapiclient.errors import HttpError
2526

2627
# If modifying these scopes, delete the file token.json.
2728
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
@@ -35,21 +36,21 @@ def main():
3536
# The file token.json stores the user's access and refresh tokens, and is
3637
# created automatically when the authorization flow completes for the first
3738
# time.
38-
try:
39-
if os.path.exists('token.json'):
40-
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
41-
# If there are no (valid) credentials available, let the user log in.
42-
if not creds or not creds.valid:
43-
if creds and creds.expired and creds.refresh_token:
44-
creds.refresh(Request())
45-
else:
46-
flow = InstalledAppFlow.from_client_secrets_file(
47-
'credentials.json', SCOPES)
48-
creds = flow.run_local_server(port=0)
49-
# Save the credentials for the next run
50-
with open('token.json', 'w') as token:
51-
token.write(creds.to_json())
39+
if os.path.exists('token.json'):
40+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
41+
# If there are no (valid) credentials available, let the user log in.
42+
if not creds or not creds.valid:
43+
if creds and creds.expired and creds.refresh_token:
44+
creds.refresh(Request())
45+
else:
46+
flow = InstalledAppFlow.from_client_secrets_file(
47+
'credentials.json', SCOPES)
48+
creds = flow.run_local_server(port=0)
49+
# Save the credentials for the next run
50+
with open('token.json', 'w') as token:
51+
token.write(creds.to_json())
5252

53+
try:
5354
service = build('calendar', 'v3', credentials=creds)
5455

5556
# Call the Calendar API
@@ -67,7 +68,7 @@ def main():
6768
start = event['start'].get('dateTime', event['start'].get('date'))
6869
print(start, event['summary'])
6970

70-
except Exception as error:
71+
except HttpError as error:
7172
print('An error occurred: %s' % error)
7273

7374

0 commit comments

Comments
 (0)