Skip to content

Commit

Permalink
Use access token for gst-plugin-spotify login
Browse files Browse the repository at this point in the history
  • Loading branch information
kingosticks committed Sep 19, 2024
1 parent e088c33 commit 1fafee3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/mopidy_spotify/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def __init__(self, *args, **kwargs):
self._credentials_dir.mkdir(mode=0o700)

def on_source_setup(self, source):
for prop in ["username", "password", "bitrate"]:
source.set_property(prop, str(self._config[prop]))
source.set_property("bitrate", str(self._config["bitrate"]))
source.set_property("cache-credentials", self._credentials_dir)
source.set_property("access-token", self.backend._web_client.token())
if self._config["allow_cache"]:
source.set_property("cache-files", self._cache_location)
source.set_property("cache-max-size", self._config["cache_size"] * 1048576)
15 changes: 14 additions & 1 deletion src/mopidy_spotify/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__( # noqa: PLR0913
self._auth = (client_id, client_secret)
else:
self._auth = None
self._access_token = None

self._base_url = base_url
self._refresh_url = refresh_url
Expand All @@ -69,6 +70,17 @@ def __init__( # noqa: PLR0913
self._cache_mutex = threading.Lock() # Protects get() cache param.
self._refresh_mutex = threading.Lock() # Protects _headers and _expires.

def token(self):
with self._refresh_mutex:
try:
if self._should_refresh_token():
self._refresh_token()
logger.info(f"Providing access token: {self._access_token}")
return self._access_token
except OAuthTokenRefreshError as e:
logger.error(e) # noqa: TRY400
return None

def get(self, path, cache=None, *args, **kwargs):
if self._authorization_failed:
logger.debug("Blocking request as previous authorization failed.")
Expand Down Expand Up @@ -149,7 +161,8 @@ def _refresh_token(self):
f"wrong token_type: {result.get('token_type')}"
)

self._headers["Authorization"] = f"Bearer {result['access_token']}"
self._access_token = result['access_token']
self._headers["Authorization"] = f"Bearer {self._access_token}"
self._expires = time.time() + result.get("expires_in", float("Inf"))

if result.get("expires_in"):
Expand Down

0 comments on commit 1fafee3

Please sign in to comment.