1717
1818import requests
1919import requests_oauthlib
20+ from oauthlib .oauth2 .rfc6749 .errors import CustomOAuth2Error
2021
2122from . import slim_click as sc
2223
@@ -74,6 +75,7 @@ def __init__(self, name: str, client_id: str = "", client_secret: str = "", auth
7475 self .tokens = {}
7576 self .device_code_url = device_code_url
7677 self .server : HTTPServer = None
78+ self .error_description : str = ""
7779
7880 def create_handler_constructor (self ):
7981 def handler (* args , ** kwargs ):
@@ -158,7 +160,10 @@ def get_tokens_from_device_flow(self) -> dict[str: str]:
158160 if "access_token" in token_response_data :
159161 return token_response_data
160162 elif "error" in token_response_data and token_response_data ["error" ] != "authorization_pending" :
161- sc .echo (f"Error: { token_response_data ['error' ]} " )
163+ sc .echo (f"Error: { token_response_data ['error' ]} " , sc .Formats .WARNING )
164+ return {}
165+ elif self .error_description :
166+ sc .echo (f"Error: { self .error_description } " , sc .Formats .WARNING )
162167 return {}
163168
164169 time .sleep (interval )
@@ -167,7 +172,7 @@ def get_tokens_from_oauth(self) -> dict[str: str]:
167172 if self .authorize_url == "" :
168173 sc .echo (f"OAuth is not available for { self .name } " )
169174 return {}
170- sc .echo (f"Opening browser to log into your { self .name } account..." )
175+ # sc.echo(f"Opening browser to log into your {self.name} account...")
171176 self .tokens = {}
172177 server_thread = threading .Thread (target = self .start_server , daemon = True )
173178 server_thread .start ()
@@ -220,9 +225,12 @@ def callback(self):
220225 parsed = urlparse (self .path )
221226 auth_code = parse_qs (parsed .query )["code" ][0 ]
222227
223- tokens = self .oauth_process .get_tokens_from_auth_code (auth_code )
224- self .oauth_process .tokens = tokens
225- self .oauth_process .shutdown_event .set ()
228+ try :
229+ tokens = self .oauth_process .get_tokens_from_auth_code (auth_code )
230+ self .oauth_process .tokens = tokens
231+ self .oauth_process .shutdown_event .set ()
232+ except CustomOAuth2Error as e :
233+ self .oauth_process .error_description = e .description
226234
227235 self .end_headers ()
228236 self .wfile .write (b"You can close this window now." )
0 commit comments