@@ -14,8 +14,8 @@ from typing import List, Optional, Set, Tuple
14
14
import dateutil .parser
15
15
import httplib2
16
16
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
19
19
20
20
try :
21
21
from googleapiclient import discovery
@@ -88,7 +88,7 @@ if not options.zulip_email:
88
88
zulip_client = zulip .init_from_options (options )
89
89
90
90
91
- def get_credentials () -> client . Credentials :
91
+ def get_credentials () -> Credentials :
92
92
"""Gets valid user credentials from storage.
93
93
94
94
If nothing has been stored, or if the stored credentials are invalid,
@@ -101,9 +101,19 @@ def get_credentials() -> client.Credentials:
101
101
try :
102
102
credential_path = os .path .join (HOME_DIR , "google-credentials.json" )
103
103
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 :
107
117
logging .exception ("Error while trying to open the `google-credentials.json` file." )
108
118
sys .exit (1 )
109
119
except OSError :
@@ -113,8 +123,7 @@ def get_credentials() -> client.Credentials:
113
123
114
124
def populate_events () -> Optional [None ]:
115
125
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 )
118
127
119
128
now = datetime .datetime .now (pytz .utc ).isoformat ()
120
129
feed = (
0 commit comments