Created
April 7, 2026 19:23
-
-
Save danvk/c7acb0c08c2c74a6c446270055139f4c to your computer and use it in GitHub Desktop.
oauth_callback_server.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """Simple localhost server to capture OAuth redirect callbacks.""" | |
| from http.server import BaseHTTPRequestHandler, HTTPServer | |
| from urllib.parse import urlparse, parse_qs | |
| PORT = 8080 | |
| class OAuthCallbackHandler(BaseHTTPRequestHandler): | |
| def do_GET(self): | |
| parsed = urlparse(self.path) | |
| params = parse_qs(parsed.query) | |
| self.send_response(200) | |
| self.send_header("Content-Type", "text/html") | |
| self.end_headers() | |
| code = params.get("code", [None])[0] | |
| token = params.get("access_token", [None])[0] | |
| error = params.get("error", [None])[0] | |
| if error: | |
| print(f"\nOAuth error: {error}") | |
| msg = f"<h1>OAuth Error</h1><p>{error}</p>" | |
| elif code: | |
| print(f"\nAuthorization code: {code}") | |
| print("Full params:", dict(params)) | |
| msg = f"<h1>Authorization code received</h1><pre>{code}</pre><p>You can close this tab.</p>" | |
| elif token: | |
| print(f"\nAccess token: {token}") | |
| msg = f"<h1>Access token received</h1><pre>{token}</pre><p>You can close this tab.</p>" | |
| else: | |
| print(f"\nCallback received. Params: {dict(params)}") | |
| msg = f"<h1>Callback received</h1><pre>{dict(params)}</pre>" | |
| self.wfile.write(msg.encode()) | |
| def log_message(self, format, *args): | |
| pass # suppress default request logging | |
| if __name__ == "__main__": | |
| server = HTTPServer(("localhost", PORT), OAuthCallbackHandler) | |
| print(f"Listening on http://localhost:{PORT}/callback") | |
| print("Press Ctrl+C to stop.") | |
| try: | |
| server.serve_forever() | |
| except KeyboardInterrupt: | |
| print("\nStopped.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment