Skip to content

Commit 3869f0e

Browse files
Niloth-ptimabbott
authored andcommitted
google-calendar: Add options --client-secret-file and --tokens-file.
For passing in custom file paths.
1 parent fe28bf3 commit 3869f0e

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

zulip/integrations/google/get-google-credentials

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ from google.auth.transport.requests import Request
88
from google.oauth2.credentials import Credentials
99
from google_auth_oauthlib.flow import InstalledAppFlow
1010

11-
# The client secret file identifies the application requesting the client's data,
12-
# and is required for the OAuth flow to fetch the tokens.
13-
# It needs to be downloaded from Google, by the user.
14-
CLIENT_SECRET_FILE = "client_secret.json" # noqa: S105
15-
HOME_DIR = os.path.expanduser("~")
16-
1711

1812
def get_credentials(
19-
tokens_path: str, scopes: List[str], noauth_local_webserver: bool = False
13+
tokens_path: str,
14+
client_secret_path: str,
15+
scopes: List[str],
16+
noauth_local_webserver: bool = False,
2017
) -> Credentials:
2118
"""
2219
Writes google tokens to a json file, using the client secret file (for the OAuth flow),
@@ -41,10 +38,9 @@ def get_credentials(
4138
if creds and creds.expired and creds.refresh_token:
4239
creds.refresh(Request())
4340
else:
44-
client_secret_path = os.path.join(HOME_DIR, CLIENT_SECRET_FILE)
4541
if not os.path.exists(client_secret_path):
4642
logging.error(
47-
"Unable to find the client secret file. Please ensure that you have downloaded the client secret file from Google, and placed it at %s.",
43+
"Unable to find the client secret file.\nPlease ensure that you have downloaded the client secret file from Google. Either place the client secret file at %s, or use the --client-secret-file option to specify the path to the client secret file.",
4844
client_secret_path,
4945
)
5046
sys.exit(1)

zulip/integrations/google/google-calendar

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ sys.path.append(os.path.join(os.path.dirname(__file__), "../../"))
1616
import zulip
1717

1818
SCOPES = ["https://www.googleapis.com/auth/calendar.events.readonly"]
19+
HOME_DIR = os.path.expanduser("~")
20+
1921
# File containing user's access and refresh tokens for Google application requests.
2022
# If it does not exist, e.g., first run, it is generated on user authorization.
2123
TOKENS_FILE = "google-tokens.json"
22-
HOME_DIR = os.path.expanduser("~")
24+
TOKENS_PATH = os.path.join(HOME_DIR, TOKENS_FILE)
25+
26+
# The client secret file identifies the application requesting the client's data,
27+
# and is required for the OAuth flow to fetch the tokens.
28+
# It needs to be downloaded from Google, by the user.
29+
CLIENT_SECRET_FILE = "client_secret.json" # noqa: S105
30+
CLIENT_SECRET_PATH = os.path.join(HOME_DIR, CLIENT_SECRET_FILE)
2331

2432
# Our cached view of the calendar, updated periodically.
2533
events: List[Tuple[int, datetime.datetime, str]] = []
@@ -32,6 +40,8 @@ sys.path.append(os.path.dirname(__file__))
3240
usage = r"""google-calendar [--config-file PATH_TO_ZULIPRC_OF_BOT]
3341
[--interval MINUTES] [--calendar CALENDAR_ID]
3442
[--channel CHANNEL_NAME] [--topic TOPIC_NAME]
43+
[--client-secret-file PATH_TO_CLIENT_SECRET_FILE]
44+
[--tokens-file PATH_TO_GOOGLE_TOKENS_FILE]
3545
[-n] [--noauth_local_webserver]
3646
3747
This integration can be used to send Zulip messages as reminders for upcoming events from your Google Calendar.
@@ -63,6 +73,18 @@ parser.add_argument(
6373
help="The topic to which to send the reminders to. Ignored if --channel is unspecified. 'calendar-reminders' is used as the default topic name.",
6474
default="calendar-reminders",
6575
)
76+
parser.add_argument(
77+
"--client-secret-file",
78+
help="The path to the file containing the client secret for the Google Calendar API. By default, the client secret file is assumed to be at {CLIENT_SECRET_PATH}.",
79+
default=CLIENT_SECRET_PATH,
80+
dest="client_secret_path",
81+
)
82+
parser.add_argument(
83+
"--tokens-file",
84+
help=f"The path to the file containing the tokens for the Google Calendar API. By default, the tokens file is generated at {TOKENS_PATH} after the first run.",
85+
default=TOKENS_PATH,
86+
dest="tokens_path",
87+
)
6688
parser.add_argument(
6789
"-n",
6890
"--noauth_local_webserver",
@@ -97,9 +119,10 @@ def get_credentials() -> Credentials:
97119
needing to be refreshed using the refresh token.
98120
"""
99121
try:
100-
tokens_path = os.path.join(HOME_DIR, TOKENS_FILE)
101122
fetch_creds = runpy.run_path("./get-google-credentials")["get_credentials"]
102-
return fetch_creds(tokens_path, SCOPES, options.noauth_local_webserver)
123+
return fetch_creds(
124+
options.tokens_path, options.client_secret_path, SCOPES, options.noauth_local_webserver
125+
)
103126
except Exception:
104127
logging.exception("Error getting google credentials")
105128
sys.exit(1)

0 commit comments

Comments
 (0)