Closed
Description
The following script fails poorly by hanging indefinitely. I believe this is because the token actually gets sent to SimpleHTTPRequestHandler, not the oauth flow server.
import http.server
import socketserver
import threading
import time
from google_auth_oauthlib import flow
CLIENT_ID = ...
CLIENT_SECRET = ...
GOOGLE_AUTH_URI = "https://accounts.google.com/o/oauth2/auth"
GOOGLE_TOKEN_URI = "https://oauth2.googleapis.com/token"
def occupy_port():
# Create an HTTP server to take up port 8080.
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", 8080), Handler) as httpd:
httpd.serve_forever()
server = threading.Thread(target=occupy_port)
server.start()
time.sleep(2)
client_config = {
"installed": {
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"redirect_uris": ["urn:ietf:wg:oauth:2.0:oob"],
"auth_uri": GOOGLE_AUTH_URI,
"token_uri": GOOGLE_TOKEN_URI,
}
}
app_flow = flow.InstalledAppFlow.from_client_config(
client_config, scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
credentials = app_flow.run_local_server(port=8080)
assert credentials is not None
server.join(timeout=1)
Instead, I would expect run_local_server
to fail fast (don't open the browser window) due to occupied port. For example, socketserver raises OSError: [Errno 48] Address already in use
. I'd expect something similar (or a more specific exception) from google-auth-oauthlib.