spotDL / spotify-downloader

Download your Spotify playlists and songs along with album art and metadata (from YouTube if a match is found).
https://spotdl.readthedocs.io/en/latest/
MIT License
17.6k stars 1.61k forks source link

Access token expired error when using spotdl as a library. #490

Closed cyberboysumanjay closed 5 years ago

cyberboysumanjay commented 5 years ago

I'm getting access token expired error when using spotdl as a library.

Error message

http status: 401, code:-1 - https://api.spotify.com/v1/tracks/6jRmczmtqIiXZBKUBHNgTn?si=u84Ru2cFRzufAPOc4qTIcg: The access token expired

SpotifyException

Traceback (most recent call last): File "/app/.heroku/python/lib/python3.6/site-packages/spotipy/client.py", line 119, in _internal_call r.raise_for_status() File "/app/.heroku/python/lib/python3.6/site-packages/requests/models.py", line 940, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.spotify.com/v1/tracks/6jRmczmtqIiXZBKUBHNgTn?si=u84Ru2cFRzufAPOc4qTIcg

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/app/.heroku/python/lib/python3.6/site-packages/spotdl/downloader.py", line 94, in init self.content, self.meta_tags = youtube_tools.match_video_and_metadata(raw_song) File "/app/.heroku/python/lib/python3.6/site-packages/spotdl/youtube_tools.py", line 55, in match_video_and_metadata meta_tags = spotify_tools.generate_metadata(track) File "/app/.heroku/python/lib/python3.6/site-packages/spotdl/spotify_tools.py", line 42, in generate_metadata meta_tags = spotify.track(raw_song) File "/app/.heroku/python/lib/python3.6/site-packages/spotipy/client.py", line 223, in track return self._get('tracks/' + trid) File "/app/.heroku/python/lib/python3.6/site-packages/spotipy/client.py", line 146, in _get return self._internal_call('GET', url, payload, kwargs) File "/app/.heroku/python/lib/python3.6/site-packages/spotipy/client.py", line 124, in _internal_call headers=r.headers) spotipy.client.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/tracks/6jRmczmtqIiXZBKUBHNgTn?si=u84Ru2cFRzufAPOc4qTIcg: The access token expired

Is there any way to refresh token programmatically using spotdl as a library?

Originally posted by @cyberboysumanjay in https://github.com/ritiek/spotify-downloader/issues/150#issuecomment-457835793

cyberboysumanjay commented 5 years ago

Any quick fix for this? @ritiek

ritiek commented 5 years ago

Take a look how it is done when using the CLI: https://github.com/ritiek/spotify-downloader/blob/a182fe5eb38a4c952fbd56a2f504aa7e5ebf0b03/spotdl/downloader.py#L204-L210

You would need to wrap your code in a try-catch block and look for the spotipy.client.SpotifyException exception which gets raised when the token expires. Then you need to regenerate the token and continue with the usual work.

(Cross-referencing #468)

cyberboysumanjay commented 5 years ago

I can confirm this is fixed by https://github.com/ritiek/spotify-downloader/pull/506