fix callback server not returning anything to the browser, implementing a way to change the content of the success page

This commit is contained in:
Matteo Fuso
2025-11-09 17:54:27 +01:00
parent 8760279c64
commit ccf625c5c5
3 changed files with 30 additions and 8 deletions

View File

@@ -76,18 +76,22 @@ session = Session.Builder() \
.create()
```
#### With auth url callback
#### With auth url callback and changing the content of the success page
```python
from librespot.core import Session
import webbrowser
# This will pass the auth url to the method
def auth_url_callback(url):
print(url)
webbrowser.open(url)
# This is the response sent to the browser once the flow has been completed successfully
success_page = "<html><body><h1>Login Successful</h1><p>You can close this window now.</p><script>setTimeout(() => {window.close()}, 100);</script></body></html>"
session = Session.Builder() \
.oauth(auth_url_callback) \
.oauth(auth_url_callback, success_page) \
.create()
```

View File

@@ -1638,7 +1638,7 @@ class Session(Closeable, MessageListener, SubListener):
pass
return self
def oauth(self, oauth_url_callback) -> Session.Builder:
def oauth(self, oauth_url_callback, success_page_content = None) -> Session.Builder:
"""
Login via OAuth
@@ -1647,7 +1647,7 @@ class Session(Closeable, MessageListener, SubListener):
"""
if os.path.isfile(self.conf.stored_credentials_file):
return self.stored_file(None)
self.login_credentials = OAuth(MercuryRequests.keymaster_client_id, "http://127.0.0.1:5588/login", oauth_url_callback).flow()
self.login_credentials = OAuth(MercuryRequests.keymaster_client_id, "http://127.0.0.1:5588/login", oauth_url_callback).set_success_page_content(success_page_content).flow()
return self
def user_pass(self, username: str, password: str) -> Session.Builder:

View File

@@ -22,11 +22,16 @@ class OAuth:
__token = ""
__server = None
__oauth_url_callback = None
__success_page_content = None
def __init__(self, client_id, redirect_url, oauth_url_callback):
self.__client_id = client_id
self.__redirect_url = redirect_url
self.__oauth_url_callback = oauth_url_callback
def set_success_page_content(self, content):
self.__success_page_content = content
return self
def __generate_generate_code_verifier(self):
possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
@@ -76,9 +81,10 @@ class OAuth:
class CallbackServer(HTTPServer):
callback_path = None
def __init__(self, server_address, RequestHandlerClass, callback_path, set_code):
def __init__(self, server_address, RequestHandlerClass, callback_path, set_code, success_page_content):
self.callback_path = callback_path
self.set_code = set_code
self.success_page_content = success_page_content
super().__init__(server_address, RequestHandlerClass)
class CallbackRequestHandler(BaseHTTPRequestHandler):
@@ -86,12 +92,23 @@ class OAuth:
if(self.path.startswith(self.server.callback_path)):
query = urllib.parse.parse_qs(urlparse(self.path).query)
if not query.__contains__("code"):
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b"Request doesn't contain 'code'")
return
self.server.set_code(query.get("code")[0])
self.wfile.write(b"librespot-python received callback")
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
success_page = self.server.success_page_content or "librespot-python received callback"
self.wfile.write(success_page.encode('utf-8'))
pass
# Suppress logging
def log_message(self, format, *args) -> None:
return
def __start_server(self):
try:
self.__server.handle_request()
@@ -106,7 +123,8 @@ class OAuth:
(url.hostname, url.port),
self.CallbackRequestHandler,
url.path,
self.set_code
self.set_code,
self.__success_page_content,
)
logging.info("OAuth: Waiting for callback on %s", url.hostname + ":" + str(url.port))
self.__start_server()