Skip to content

Commit aad6736

Browse files
Niloth-ptimabbott
authored andcommitted
google-calendar: Call get-google-credentials script internally.
The auth tokens expire every hour. So, the google-calendar script would stop functioning one hour after start, if it only loads from the token file. The get-google-credential script needs to be called directly from the google-calendar script, without the user needing to run that as a separate command. The get-google-credentials script is not a module. And it uses hyphens in its name, hence it cannot be directly imported into google-calendar. To avoid renaming files, and smoothly pass in arguments, runpy is used. Though google-calendar script is currently the only google service integration that uses get-google-credentials, the script is not merged with the google-calendar script, to keep the logic separate, and easy to expand. The get-google-credentials script can no longer be run directly, as the get_credentials() arguments do not have any default values, with the constants deleted, to avoid redundancy with the google-calendar script. Updated the function docstrings, usage help and error messages.
1 parent 3d85751 commit aad6736

File tree

2 files changed

+12
-28
lines changed

2 files changed

+12
-28
lines changed

zulip/integrations/google/get-google-credentials

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@
22
import logging
33
import os
44
import sys
5+
from typing import List
56

67
from google.auth.transport.requests import Request
78
from google.oauth2.credentials import Credentials
89
from google_auth_oauthlib.flow import InstalledAppFlow
910

10-
SCOPES = ["https://www.googleapis.com/auth/calendar.events.readonly"]
11-
# File containing user's access and refresh tokens for Google application requests.
12-
# If it does not exist, e.g., first run, it is generated on user authorization.
13-
TOKENS_FILE = "google-tokens.json"
1411
# The client secret file identifies the application requesting the client's data,
1512
# and is required for the OAuth flow to fetch the tokens.
1613
# It needs to be downloaded from Google, by the user.
1714
CLIENT_SECRET_FILE = "client_secret.json" # noqa: S105
1815
HOME_DIR = os.path.expanduser("~")
1916

2017

21-
def get_credentials() -> Credentials:
18+
def get_credentials(tokens_path: str, scopes: List[str]) -> Credentials:
2219
"""
2320
Writes google tokens to a json file, using the client secret file (for the OAuth flow),
2421
and the refresh token.
@@ -33,12 +30,9 @@ def get_credentials() -> Credentials:
3330
3431
The fetched tokens are written to storage in a json file, for reference by other scripts.
3532
"""
36-
3733
creds = None
38-
tokens_path = os.path.join(HOME_DIR, TOKENS_FILE)
39-
4034
if os.path.exists(tokens_path):
41-
creds = Credentials.from_authorized_user_file(tokens_path, SCOPES)
35+
creds = Credentials.from_authorized_user_file(tokens_path, scopes)
4236
if not creds or not creds.valid:
4337
if creds and creds.expired and creds.refresh_token:
4438
creds.refresh(Request())
@@ -50,11 +44,8 @@ def get_credentials() -> Credentials:
5044
client_secret_path,
5145
)
5246
sys.exit(1)
53-
flow = InstalledAppFlow.from_client_secrets_file(client_secret_path, SCOPES)
47+
flow = InstalledAppFlow.from_client_secrets_file(client_secret_path, scopes)
5448
creds = flow.run_local_server(port=0)
5549
with open(tokens_path, "w") as token:
5650
token.write(creds.to_json())
5751
return creds
58-
59-
60-
get_credentials()

zulip/integrations/google/google-calendar

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import datetime
44
import itertools
55
import logging
66
import os
7+
import runpy
78
import sys
89
import time
910
from typing import List, Optional, Set, Tuple
@@ -34,8 +35,6 @@ usage = r"""google-calendar --user EMAIL [--interval MINUTES] [--calendar CALEND
3435
3536
Specify your Zulip API credentials and server in a ~/.zuliprc file, or using the options.
3637
37-
Before running this integration, make sure you download the client secret file from Google, and run the get-google-credentials script to give Zulip read access to your Google Calendar.
38-
3938
This integration should be run on your local machine, as your API key is accessible to local users through the command line.
4039
4140
For more information, see https://zulip.com/integrations/doc/google-calendar.
@@ -78,23 +77,17 @@ if options.verbose:
7877

7978

8079
def get_credentials() -> Credentials:
81-
"""Gets valid user credentials from storage.
82-
83-
If nothing has been stored, or if the stored credentials are invalid,
84-
an exception is thrown and the user is informed to run the script in this directory to get
85-
credentials.
80+
"""Fetches credentials using the get-google-credentials script.
8681
87-
Returns:
88-
Credentials, the obtained credential.
82+
Needs to call get-google-credentials everytime, because the auth token expires every hour,
83+
needing to be refreshed using the refresh token.
8984
"""
9085
try:
9186
tokens_path = os.path.join(HOME_DIR, TOKENS_FILE)
92-
return Credentials.from_authorized_user_file(tokens_path, SCOPES)
93-
except ValueError:
94-
logging.exception("Error while trying to open the %s file.", TOKENS_FILE)
95-
sys.exit(1)
96-
except OSError:
97-
logging.error("Run the get-google-credentials script from this directory first.")
87+
fetch_creds = runpy.run_path("./get-google-credentials")["get_credentials"]
88+
return fetch_creds(tokens_path, SCOPES)
89+
except Exception:
90+
logging.exception("Error getting google credentials")
9891
sys.exit(1)
9992

10093

0 commit comments

Comments
 (0)