Support Rust librespot credential format in stored_file()

Adds fallback logic to load credentials from Rust librespot's OAuth flow.
The stored_file() method now handles both Python and Rust credential formats.

Related to #277
This commit is contained in:
Daniel Moen
2025-09-30 18:42:33 +02:00
parent 1ad1e40b5a
commit 87171a3d29
2 changed files with 22 additions and 1 deletions

View File

@@ -91,6 +91,18 @@ session = Session.Builder() \
.create() .create()
``` ```
### Use Stored Credentials for Login
```python
from librespot.core import Session
# Supports both Python and Rust librespot credential formats
session = Session.Builder() \
.stored_file("/path/to/credentials.json") \
.create()
```
### Get Spotify's OAuth token ### Get Spotify's OAuth token
```python ```python

View File

@@ -1618,6 +1618,7 @@ class Session(Closeable, MessageListener, SubListener):
pass pass
else: else:
try: try:
# Try Python librespot format first
self.login_credentials = Authentication.LoginCredentials( self.login_credentials = Authentication.LoginCredentials(
typ=Authentication.AuthenticationType.Value( typ=Authentication.AuthenticationType.Value(
obj["type"]), obj["type"]),
@@ -1625,7 +1626,15 @@ class Session(Closeable, MessageListener, SubListener):
auth_data=base64.b64decode(obj["credentials"]), auth_data=base64.b64decode(obj["credentials"]),
) )
except KeyError: except KeyError:
pass # Try Rust librespot format (auth_type as int, auth_data instead of credentials)
try:
self.login_credentials = Authentication.LoginCredentials(
typ=obj["auth_type"],
username=obj["username"],
auth_data=base64.b64decode(obj["auth_data"]),
)
except KeyError:
pass
return self return self
def oauth(self, oauth_url_callback) -> Session.Builder: def oauth(self, oauth_url_callback) -> Session.Builder: