Readme and implementation changes

Added optional auth url callback
Removed user pass from readme
Added oauth to readme
This commit is contained in:
werwolf2303
2025-06-11 11:30:52 +02:00
parent f210850bee
commit 22e6419bc9
3 changed files with 46 additions and 6 deletions

View File

@@ -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")

View File

@@ -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:

View File

@@ -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