Skip to content

Commit

Permalink
Feat: Handle google.auth.exceptions.RefreshError.
Browse files Browse the repository at this point in the history
  • Loading branch information
theofficialvedantjoshi committed Feb 22, 2025
1 parent 12864e8 commit 6a04715
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
11 changes: 10 additions & 1 deletion zulip/integrations/google/get-google-credentials
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin/env python3
import argparse
import logging
import os
import sys

from google.auth.exceptions import RefreshError # type: ignore[import-not-found]
from google.auth.transport.requests import Request # type: ignore[import-not-found]
from google.oauth2.credentials import Credentials # type: ignore[import-not-found]
from google_auth_oauthlib.flow import InstalledAppFlow
Expand Down Expand Up @@ -53,7 +56,13 @@ def get_credentials() -> Credentials:
credentials = Credentials.from_authorized_user_file(credential_path, SCOPES)
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
try:
credentials.refresh(Request())
except RefreshError:
logging.error(
"The credentials have expired. Generate a new client_secret.json file."
)
sys.exit(1)
else:
flow = InstalledAppFlow.from_client_secrets_file(
os.path.join(HOME_DIR, CLIENT_SECRET_FILE), SCOPES
Expand Down
27 changes: 17 additions & 10 deletions zulip/integrations/google/google-calendar
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import dateutil.parser
import pytz

try:
from google.auth.exceptions import RefreshError # type: ignore[import-not-found]
from google.oauth2.credentials import Credentials # type: ignore[import-not-found]
from googleapiclient.discovery import build
except ImportError:
Expand Down Expand Up @@ -125,17 +126,23 @@ def get_credentials() -> Credentials:
def populate_events() -> Optional[None]:
creds = get_credentials()
service = build("calendar", "v3", credentials=creds)
feed = (
service.events()
.list(
calendarId=options.calendarID,
timeMin=datetime.datetime.now(pytz.utc).isoformat(),
timeMax=datetime.datetime.now(pytz.utc).isoformat().split("T")[0] + "T23:59:59Z",
singleEvents=True,
orderBy="startTime",
try:
feed = (
service.events()
.list(
calendarId=options.calendarID,
timeMin=datetime.datetime.now(pytz.utc).isoformat(),
timeMax=datetime.datetime.now(pytz.utc).isoformat().split("T")[0] + "T23:59:59Z",
singleEvents=True,
orderBy="startTime",
)
.execute()
)
.execute()
)
except RefreshError:
logging.error(
"The credentials have expired. Generate a new client_secret.json file and run the get-google-credentials script."
)
sys.exit(1)
events.clear()
for event in feed["items"]:
try:
Expand Down

0 comments on commit 6a04715

Please sign in to comment.