2
2
import argparse
3
3
import os
4
4
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
7
8
8
- flags = argparse .ArgumentParser (parents = [tools .argparser ]).parse_args ()
9
+ flags = argparse .ArgumentParser (description = "Google Calendar Bot" )
10
+ flags .add_argument ('--noauth_local_webserver' , action = 'store_true' ,
11
+ help = 'Run OAuth flow in console instead of opening a web browser.' )
12
+ args = flags .parse_args ()
9
13
10
14
# If modifying these scopes, delete your previously saved credentials
11
15
# at zulip/bots/gcal/
12
16
# NOTE: When adding more scopes, add them after the previous one in the same field, with a space
13
17
# seperating them.
14
- SCOPES = "https://www.googleapis.com/auth/calendar.readonly"
18
+ SCOPES = [ "https://www.googleapis.com/auth/calendar.readonly" ]
15
19
# This file contains the information that google uses to figure out which application is requesting
16
20
# this client's data.
17
21
CLIENT_SECRET_FILE = "client_secret.json" # noqa: S105
18
22
APPLICATION_NAME = "Zulip Calendar Bot"
19
23
HOME_DIR = os .path .expanduser ("~" )
24
+ CREDENTIALS_PATH = os .path .join (HOME_DIR , "google-credentials.json" )
20
25
21
26
22
- def get_credentials () -> client . Credentials :
27
+ def get_credentials () -> Credentials :
23
28
"""Gets valid user credentials from storage.
24
29
25
30
If nothing has been stored, or if the stored credentials are invalid,
@@ -29,18 +34,30 @@ def get_credentials() -> client.Credentials:
29
34
Credentials, the obtained credential.
30
35
"""
31
36
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 )
37
+ creds = None
38
+
39
+ # Check if the credentials file exists
40
+ if os .path .exists (CREDENTIALS_PATH ):
41
+ creds = Credentials .from_authorized_user_file (CREDENTIALS_PATH , SCOPES )
42
+
43
+ # If there are no valid credentials, initiate the OAuth flow
44
+ if not creds or not creds .valid :
45
+ if creds and creds .expired and creds .refresh_token :
46
+ creds .refresh (Request ())
47
+ else :
48
+ flow = InstalledAppFlow .from_client_secrets_file (
49
+ os .path .join (HOME_DIR , CLIENT_SECRET_FILE ), SCOPES
50
+ )
51
+ if args .noauth_local_webserver :
52
+ creds = flow .run_console ()
53
+ else :
54
+ creds = flow .run_local_server (port = 0 )
55
+
56
+ # Save the credentials for future use
57
+ with open (CREDENTIALS_PATH , 'w' ) as token_file :
58
+ token_file .write (creds .to_json ())
59
+
60
+ print ("Storing credentials to " + CREDENTIALS_PATH )
44
61
45
62
46
63
get_credentials ()
0 commit comments