22import argparse
33import os
44
5- from oauth2client import client , tools
6- from oauth2client .file import Storage
5+ from google .auth .transport .requests import Request
6+ from google .oauth2 .credentials import Credentials
7+ from google_auth_oauthlib .flow import InstalledAppFlow
78
8- flags = argparse .ArgumentParser (parents = [tools .argparser ]).parse_args ()
9+ flags = argparse .ArgumentParser (description = "Google Calendar Bot" )
10+ flags .add_argument (
11+ "--noauth_local_webserver" ,
12+ action = "store_true" ,
13+ help = "Run OAuth flow in console instead of opening a web browser." ,
14+ )
15+ args = flags .parse_args ()
916
1017# If modifying these scopes, delete your previously saved credentials
1118# at zulip/bots/gcal/
1219# NOTE: When adding more scopes, add them after the previous one in the same field, with a space
1320# seperating them.
14- SCOPES = "https://www.googleapis.com/auth/calendar.readonly"
21+ SCOPES = [ "https://www.googleapis.com/auth/calendar.readonly" ]
1522# This file contains the information that google uses to figure out which application is requesting
1623# this client's data.
1724CLIENT_SECRET_FILE = "client_secret.json" # noqa: S105
1825APPLICATION_NAME = "Zulip Calendar Bot"
1926HOME_DIR = os .path .expanduser ("~" )
27+ CREDENTIALS_PATH = os .path .join (HOME_DIR , "google-credentials.json" )
2028
21-
22- def get_credentials () -> client .Credentials :
29+ def get_credentials () -> Credentials :
2330 """Gets valid user credentials from storage.
2431
2532 If nothing has been stored, or if the stored credentials are invalid,
@@ -29,18 +36,32 @@ def get_credentials() -> client.Credentials:
2936 Credentials, the obtained credential.
3037 """
3138
32- credential_path = os .path .join (HOME_DIR , "google-credentials.json" )
33-
34- store = Storage (credential_path )
35- credentials = store .get ()
36- if not credentials or credentials .invalid :
37- flow = client .flow_from_clientsecrets (os .path .join (HOME_DIR , CLIENT_SECRET_FILE ), SCOPES )
38- flow .user_agent = APPLICATION_NAME
39- # This attempts to open an authorization page in the default web browser, and asks the user
40- # to grant the bot access to their data. If the user grants permission, the run_flow()
41- # function returns new credentials.
42- credentials = tools .run_flow (flow , store , flags )
43- print ("Storing credentials to " + credential_path )
39+ creds = None
40+
41+ # Check if the credentials file exists
42+ if os .path .exists (CREDENTIALS_PATH ):
43+ creds = Credentials .from_authorized_user_file (CREDENTIALS_PATH , SCOPES )
44+
45+ # If there are no valid credentials, initiate the OAuth flow
46+ if not creds or not creds .valid :
47+ if creds and creds .expired and creds .refresh_token :
48+ creds .refresh (Request ())
49+ else :
50+ flow = InstalledAppFlow .from_client_secrets_file (
51+ os .path .join (HOME_DIR , CLIENT_SECRET_FILE ), SCOPES
52+ )
53+ if args .noauth_local_webserver :
54+ creds = flow .run_console ()
55+ else :
56+ creds = flow .run_local_server (port = 0 )
57+
58+ # Save the credentials for future use
59+ with open (CREDENTIALS_PATH , "w" ) as token_file :
60+ token_file .write (creds .to_json ())
61+
62+ print ("Storing credentials to " + CREDENTIALS_PATH )
63+
64+ return creds # Return the obtained credentials
4465
4566
4667get_credentials ()
0 commit comments