spotcast: Access Token expired when trying to call spotify API

Indeed, it fails every hour, but it’s not exactly every 60 minutes.

2025-05-19 12:49:36.195 ERROR (SyncWorker_7) [spotipy.client] HTTP Error for GET to https://api.spotify.com/v1/me/tracks with Params: {'limit': 1, 'offset': 0, 'market': None} returned 401 due to The access token expired
2025-05-19 13:42:43.467 ERROR (SyncWorker_2) [spotipy.client] HTTP Error for GET to https://api.spotify.com/v1/me/playlists with Params: {'limit': 1, 'offset': 0} returned 401 due to The access token expired
2025-05-19 13:45:43.474 ERROR (SyncWorker_2) [spotipy.client] HTTP Error for GET to https://api.spotify.com/v1/me/tracks with Params: {'limit': 1, 'offset': 0, 'market': None} returned 401 due to The access token expired
2025-05-19 13:48:37.392 ERROR (SyncWorker_4) [spotipy.client] HTTP Error for GET to https://api.spotify.com/v1/me/playlists with Params: {'limit': 1, 'offset': 0} returned 401 due to The access token expired
2025-05-19 13:49:37.410 ERROR (SyncWorker_5) [spotipy.client] HTTP Error for GET to https://api.spotify.com/v1/me/tracks with Params: {'limit': 1, 'offset': 0, 'market': None} returned 401 due to The access token expired

Originally posted by @Mincka in #543

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 27

Most upvoted comments

Hello, Still running without any error. That’s great! I think this one can be closed. Thank you.

build 14 released. Passing hassfest

can you move to the newest build I aligned the expiration offset between public and private api.

@Mincka actually scratch that. I’ll have to make some tweaks, hassfest doesn’t like one of the string definition added for the fisrt validation step

looks a lot like the issue is with the public API. I’ll implement a tighter offset on the expiration just like I did for the desktop token

think I found the issue, I wrote

    @property
    def valid_token(self) -> bool:
        """Returns True if the token is still valid."""
        return self.expires_at > time() + self.EXPIRATION_OFFSET

instead of

    @property
    def valid_token(self) -> bool:
        """Returns True if the token is still valid."""
        return self.expires_at + self.EXPIRATION_OFFSET > time()

I was basically giving 10 minutes over the expiration time to the token, no wonder you kept having the issue.