From 22e6419bc95392424b62c3eb38a1b5ab35448aee Mon Sep 17 00:00:00 2001 From: werwolf2303 Date: Wed, 11 Jun 2025 11:30:52 +0200 Subject: [PATCH] Readme and implementation changes Added optional auth url callback Removed user pass from readme Added oauth to readme --- README.md | 33 +++++++++++++++++++++++++++++++-- librespot/core.py | 10 ++++++++-- librespot/oauth.py | 9 +++++++-- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 040e83a..cce95a3 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,35 @@ from librespot.zeroconf import ZeroconfServer zeroconf = ZeroconfServer.Builder().create() ``` +### Use OAuth for Login + +#### Without auth url callback + +```python +from librespot.core import Session + +# This will log an url in the terminal that you have to open + +session = Session.Builder() \ + .oauth(None) \ + .create() +``` + +#### With auth url callback + +```python +from librespot.core import Session + +# This will pass the auth url to the method + +def auth_url_callback(url): + print(url) + +session = Session.Builder() \ + .oauth(auth_url_callback) \ + .create() +``` + ### Get Spotify's OAuth token ```python @@ -69,7 +98,7 @@ from librespot.core import Session session = Session.Builder() \ - .user_pass("Username", "Password") \ + .oauth(None) \ .create() access_token = session.tokens().get("playlist-read") @@ -85,7 +114,7 @@ from librespot.metadata import TrackId from librespot.audio.decoders import AudioQuality, VorbisOnlyAudioQuality session = Session.Builder() \ - .user_pass("Username", "Password") \ + .oauth(None) \ .create() track_id = TrackId.from_uri("spotify:track:xxxxxxxxxxxxxxxxxxxxxx") diff --git a/librespot/core.py b/librespot/core.py index c2c3c49..cec489e 100644 --- a/librespot/core.py +++ b/librespot/core.py @@ -1596,10 +1596,16 @@ class Session(Closeable, MessageListener, SubListener): pass return self - def oauth(self) -> Session.Builder: + def oauth(self, oauth_url_callback) -> Session.Builder: + """ + Login via OAuth + + You can supply an oauth_url_callback method that takes a string and returns the OAuth URL. + When oauth_url_callback is None, this will block until logged in. + """ 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").flow() + self.login_credentials = OAuth(MercuryRequests.keymaster_client_id, "http://127.0.0.1:5588/login", oauth_url_callback).flow() return self def user_pass(self, username: str, password: str) -> Session.Builder: diff --git a/librespot/oauth.py b/librespot/oauth.py index 3e71238..fa90656 100644 --- a/librespot/oauth.py +++ b/librespot/oauth.py @@ -21,10 +21,12 @@ class OAuth: __code = "" __token = "" __server = None + __oauth_url_callback = None - def __init__(self, client_id, redirect_url): + 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 __generate_generate_code_verifier(self): possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" @@ -39,7 +41,10 @@ class OAuth: def get_auth_url(self): self.__code_verifier = self.__generate_generate_code_verifier() - return self.__spotify_auth % (self.__client_id, self.__redirect_url, self.__generate_code_challenge(self.__code_verifier), "+".join(self.__scopes)) + auth_url = self.__spotify_auth % (self.__client_id, self.__redirect_url, self.__generate_code_challenge(self.__code_verifier), "+".join(self.__scopes)) + if self.__oauth_url_callback: + self.__oauth_url_callback(auth_url) + return auth_url def set_code(self, code): self.__code = code