简体   繁体   中英

Why do I always get "JSON Decode Error" when using spotipy?

I just started working with the Python API for Spotify 'spotipy'. My goal is to get the song IDs in the first step and retrieve the features of a song in the second step using the IDs. I already had some issues getting the IDs which I described here .

After some tweaking I always got the error:

JSONDecodeError: Expecting value

when searching for the song IDs. I then executed the script on the next day and somehow it worked out fine, but got stuck at around 80%. Fortunately, I cached the IDs in a separate file and now I want to try to get the song features of those songs. I use the following code:

import pandas as pd
import api_config
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=api_config.APP_CLIENT_ID, client_secret=api_config.APP_CLIENT_SECRET))        

def readChartIDs():
    
    chartIDs = pd.read_csv("../IDs.csv")
    chartIDs = chartIDs.dropna()
    
    return chartIDs

chartIDs = readChartIDs()
testSample = chartIDs.sample(10, axis=0, ignore_index=True)
testSample = testSample['IDs'].values.tolist()
song = sp.tracks(testSample)

but I get the same Error:


  File "...\getFeatures.py", line 28, in <module>
    song = sp.tracks(testSample)

  File "...\anaconda3\lib\site-packages\spotipy\client.py", line 356, in tracks
    return self._get("tracks/?ids=" + ",".join(tlist), market=market)

  File "...\anaconda3\lib\site-packages\spotipy\client.py", line 297, in _get
    return self._internal_call("GET", url, payload, kwargs)

  File "...\anaconda3\lib\site-packages\spotipy\client.py", line 221, in _internal_call
    headers = self._auth_headers()

  File "...\anaconda3\lib\site-packages\spotipy\client.py", line 212, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)

  File "...\anaconda3\lib\site-packages\spotipy\oauth2.py", line 234, in get_access_token
    token_info = self.cache_handler.get_cached_token()

  File "...\anaconda3\lib\site-packages\spotipy\cache_handler.py", line 78, in get_cached_token
    token_info = json.loads(token_info_string)

  File "...\anaconda3\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)

  File "...\anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())

  File "...\anaconda3\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value

I don't understand how this error can occur in the first way, because I don't have any access to the spotipy code. Could it be a bug or maybe some wrong.network settings?

I use Python 3.8.3 on Windows 10 with Spyder 4.1.4 and spotipy 2.22.0.

I already tried to get the track data of a single track, but the error remains the same. Restarting PC or router didn't help either. I also tried to reinstall spotipy and Python but nothing helped. To exclude issues with authorization, I also created a new app on the Spotify API dashboard and the error is still the same.

Just after posting I found the answer to the problem. The file.cache which is controlled by git was changed and got the head tag in it. In this file the access token is stored which is mentioned in the error description.

Deleting the head tag and solving any merging issues from git or deleting the.cache file, solves the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM