Skip to content

Commit

Permalink
added error handling for gmail authentication
Browse files Browse the repository at this point in the history
davidenders11 committed Oct 30, 2023
1 parent bcf6fd3 commit 81fa09c
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion gmailwrapper.py
Original file line number Diff line number Diff line change
@@ -35,7 +35,14 @@ def auth(self):
# created automatically when the authorization flow completes for the first time.
if os.path.exists("token.json"):
self.logger.info("Reading credentials from existing token.json")
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
try:
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
except Exception as e:
self.logger.error(f"Error reading credentials from token.json: {e}\nRemoving token.json and re-authenticating")
os.remove("token.json")
creds = None
pass

# 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:
@@ -131,6 +138,7 @@ def draft(self, subject, content, other, thread_id=None):
# add thread id if replying
if thread_id:
body["threadId"] = str(thread_id)
print(body)

draft = (
self.service.users().drafts().create(userId="me", body=body).execute()

0 comments on commit 81fa09c

Please sign in to comment.