Skip to content

Commit 1666a0c

Browse files
Replace oauth2client with google-auth and google-auth-oauthlib in google-calendar integration
1 parent 8c27331 commit 1666a0c

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

Diff for: zulip/integrations/google/google-calendar

+17-8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ from typing import List, Optional, Set, Tuple
1414
import dateutil.parser
1515
import httplib2
1616
import pytz
17-
from oauth2client import client
18-
from oauth2client.file import Storage
17+
from google.oauth2.credentials import Credentials
18+
from google_auth_oauthlib.flow import InstalledAppFlow
1919

2020
try:
2121
from googleapiclient import discovery
@@ -88,7 +88,7 @@ if not options.zulip_email:
8888
zulip_client = zulip.init_from_options(options)
8989

9090

91-
def get_credentials() -> client.Credentials:
91+
def get_credentials() -> Credentials:
9292
"""Gets valid user credentials from storage.
9393
9494
If nothing has been stored, or if the stored credentials are invalid,
@@ -101,9 +101,19 @@ def get_credentials() -> client.Credentials:
101101
try:
102102
credential_path = os.path.join(HOME_DIR, "google-credentials.json")
103103

104-
store = Storage(credential_path)
105-
return store.get()
106-
except client.Error:
104+
if os.path.exists(credential_path):
105+
# Load credentials from the file
106+
credentials = Credentials.from_authorized_user_file(credential_path, SCOPES)
107+
else:
108+
# Run the OAuth flow to get new credentials
109+
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
110+
credentials = flow.run_local_server(port=0)
111+
# Save the credentials for the next run
112+
with open(credential_path, "w") as token:
113+
token.write(credentials.to_json())
114+
115+
return credentials
116+
except FileNotFoundError:
107117
logging.exception("Error while trying to open the `google-credentials.json` file.")
108118
sys.exit(1)
109119
except OSError:
@@ -113,8 +123,7 @@ def get_credentials() -> client.Credentials:
113123

114124
def populate_events() -> Optional[None]:
115125
credentials = get_credentials()
116-
creds = credentials.authorize(httplib2.Http())
117-
service = discovery.build("calendar", "v3", http=creds)
126+
service = discovery.build("calendar", "v3", credentials=credentials)
118127

119128
now = datetime.datetime.now(pytz.utc).isoformat()
120129
feed = (

Diff for: zulip/integrations/google/requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
httplib2>=0.22.0
2-
oauth2client>=4.1.3
2+
google-auth>=2.0.0
3+
google-auth-oauthlib>=0.4.6
4+
google-api-python-client>=2.0.0

Diff for: zulip_bots/zulip_bots/bots/google_search/google_search.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def google_search(keywords: str) -> List[Dict[str, str]]:
1818
# Gets all search URLs
1919
search = soup.find(id="search")
2020
assert isinstance(search, Tag)
21-
anchors = search.findAll("a")
21+
anchors = search.find_all("a")
2222
results = []
2323

2424
for a in anchors:

Diff for: zulip_bots/zulip_bots/bots/twitpost/twitpost.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def usage(self) -> str:
2323

2424
def initialize(self, bot_handler: AbstractBotHandler) -> None:
2525
self.config_info = bot_handler.get_config_info("twitter")
26-
auth = tweepy.OAuthHandler(
26+
auth = tweepy.OAuth1UserHandler(
2727
self.config_info["consumer_key"], self.config_info["consumer_secret"]
2828
)
2929
auth.set_access_token(

0 commit comments

Comments
 (0)