You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Spotipy调用Spotify API时持续出现JSONDecodeError求助

Spotify API(Spotipy)JSONDecodeError问题

问题背景

刚上手Spotify的Python API库Spotipy,计划先获取歌曲ID,再通过ID提取歌曲特征。此前获取ID阶段就遇到过问题,调整后仍触发以下错误:

JSONDecodeError: Expecting value

次日执行脚本时问题暂时消失,但运行至约80%时中断。已将ID缓存至本地文件,现在尝试用缓存ID获取歌曲特征,仍触发相同错误。

复现代码

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)

完整报错栈

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

已尝试的解决措施

  • 尝试获取单条曲目数据
  • 重启电脑与路由器
  • 重装Spotipy和Python
  • 在Spotify API控制台创建新应用

环境信息

  • 操作系统:Windows10
  • Python版本:3.8.3
  • IDE:Spyder4.1.4
  • Spotipy版本:2.22.0

内容的提问来源于stack exchange,提问作者KaeptnBlaubaer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 12:25:22