2121from .oauth2 import Client
2222
2323
24+ logger = logging .getLogger (__file__ )
25+
2426def obtain_auth_code (listen_port , auth_uri = None ):
2527 """This function will start a web server listening on http://localhost:port
2628 and then you need to open a browser on this device and visit your auth_uri.
@@ -34,10 +36,13 @@ def obtain_auth_code(listen_port, auth_uri=None):
3436 :param auth_uri: If provided, this function will try to open a local browser.
3537 :return: Hang indefinitely, until it receives and then return the auth code.
3638 """
39+ exit_hint = "Visit http://localhost:{p}?code=exit to abort" .format (p = listen_port )
40+ logger .warn (exit_hint )
3741 if auth_uri :
3842 page = "http://localhost:{p}?{q}" .format (p = listen_port , q = urlencode ({
3943 "text" : "Open this link to sign in. You may use incognito window" ,
4044 "link" : auth_uri ,
45+ "exit_hint" : exit_hint ,
4146 }))
4247 browse (page )
4348 server = HTTPServer (("" , int (listen_port )), AuthCodeReceiver )
@@ -61,7 +66,7 @@ def browse(auth_uri):
6166 break
6267 except webbrowser .Error :
6368 pass # This browser is not installed. Try next one.
64- logging .info ("Please open a browser on THIS device to visit: %s" % auth_uri )
69+ logger .info ("Please open a browser on THIS device to visit: %s" % auth_uri )
6570 controller .open (auth_uri )
6671
6772class AuthCodeReceiver (BaseHTTPRequestHandler ):
@@ -74,8 +79,11 @@ def do_GET(self):
7479 self ._send_full_response ('Authcode:\n {}' .format (ac ))
7580 # NOTE: Don't do self.server.shutdown() here. It'll halt the server.
7681 elif qs .get ('text' ) and qs .get ('link' ): # Then display a landing page
77- self ._send_full_response ('<a href={link}>{text}</a>' .format (
78- link = qs ['link' ][0 ], text = qs ['text' ][0 ]))
82+ self ._send_full_response (
83+ '<a href={link}>{text}</a><hr/>{exit_hint}' .format (
84+ link = qs ['link' ][0 ], text = qs ['text' ][0 ],
85+ exit_hint = qs .get ("exit_hint" , ['' ])[0 ],
86+ ))
7987 else :
8088 self ._send_full_response ("This web service serves your redirect_uri" )
8189
0 commit comments