使用Spotipy获取Spotify播放列表时遭遇JSONDecodeError
Spotipy调用接口JSONDecodeError问题排查与解决
问题场景
已按照Spotipy官方指引完成Spotify开发者应用配置,确认Client ID、Client Secret无误,但调用接口时抛出JSONDecodeError: Expecting value: line 1 column 1 (char 0)。
代码示例
import spotipy from spotipy.oauth2 import SpotifyOAuth YOUR_APP_CLIENT_ID = "myappclientid" YOUR_APP_CLIENT_SECRET = "myclientsecret" YOUR_APP_REDIRECT_URI = "mycallback" SCOPE = "playlist-read-private" sp = spotipy.Spotify( auth_manager=SpotifyOAuth( client_id=YOUR_APP_CLIENT_ID, client_secret=YOUR_APP_CLIENT_SECRET, redirect_uri=YOUR_APP_REDIRECT_URI, scope=SCOPE, ) ) sp.current_user_playlists(limit = 50)
完整报错回溯
--------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) c:\my_spotify_playlists\example.py in 14 ) 15 ---> 16 sp.current_user_playlists(limit = 50) c:\Users\MyUserName\AppData\Local\Programs\Python\Python38\lib\site-packages\spotipy\client.py in current_user_playlists(self, limit, offset) 635 - offset - the index of the first item to return 636 """ --> 637 return self._get("me/playlists", limit=limit, offset=offset) 638 639 def playlist(self, playlist_id, fields=None, market=None, additional_types=("track",)): c:\Users\MyUserName\AppData\Local\Programs\Python\Python38\lib\site-packages\spotipy\client.py in _get(self, url, args, payload, **kwargs) 321 kwargs.update(args) 322 --> 323 return self._internal_call("GET", url, payload, kwargs) 324 325 def _post(self, url, args=None, payload=None, **kwargs): c:\Users\MyUserName\AppData\Local\Programs\Python\Python38\lib\site-packages\spotipy\client.py in _internal_call(self, method, url, payload, params) 245 if not url.startswith("http"): 246 url = self.prefix + url --> 247 headers = self._auth_headers() 248 249 if "content_type" in args["params"]: ... 354 except StopIteration as err: --> 355 raise JSONDecodeError("Expecting value", s, err.value) from None 356 return obj, end JSONDecodeError: Expecting value: line 1 column 1 (char 0)
错误原因分析
该错误是Spotipy尝试解析API响应时,接收到空内容或非JSON格式数据导致的,常见触发因素:
- 重定向URI与开发者控制台配置不匹配(完全一致是硬性要求,包括协议、路径、斜杠)
- 网络环境无法正常连接Spotify服务器,返回空响应
- 本地认证缓存文件损坏,导致授权流程异常
- 代理配置未生效,请求无法到达Spotify服务器
解决方案
严格核对重定向URI
进入Spotify开发者控制台,打开应用的Edit Settings页面,确保Redirect URIs列表中的地址与代码中YOUR_APP_REDIRECT_URI完全一致,比如代码中是http://localhost:8080/callback,控制台必须一字不差,不能省略协议或多/少斜杠。清理认证缓存
删除项目目录下自动生成的.cache文件,重新运行代码并完成授权流程,避免损坏的缓存导致认证失败。检查网络连通性
测试能否访问https://api.spotify.com和https://accounts.spotify.com,如果无法访问,需调整网络环境;若使用代理,可设置环境变量:# Windows set HTTP_PROXY=http://your-proxy:port set HTTPS_PROXY=http://your-proxy:port # Linux/macOS export HTTP_PROXY=http://your-proxy:port export HTTPS_PROXY=http://your-proxy:port显式指定缓存路径(可选)
在SpotifyOAuth中添加cache_path参数,明确指定缓存文件位置,避免路径问题:sp = spotipy.Spotify( auth_manager=SpotifyOAuth( client_id=YOUR_APP_CLIENT_ID, client_secret=YOUR_APP_CLIENT_SECRET, redirect_uri=YOUR_APP_REDIRECT_URI, scope=SCOPE, cache_path=".spotify_auth_cache" ) )开启调试模式定位问题
初始化Spotify客户端时开启调试,查看请求和响应详情:sp = spotipy.Spotify( auth_manager=SpotifyOAuth(...), debug=True, requests_timeout=10 )
内容的提问来源于stack exchange,提问作者dko512
相关产品推荐
相关产品推荐

