调用Spotify API创建播放列表时出现403错误,求排查解决
调用Spotify API创建播放列表时出现403错误
执行命令 python3 spotifyv2.py {user_name} 运行代码时,返回如下403错误:
HTTP Error for POST to https://api.spotify.com/v1/users/test/playlists with Params: {} returned 403 due to You cannot create a playlist for another user Traceback (most recent call last): File "/home/joachim/Jarvis/venv/lib/python3.12/site-packages/spotipy/client.py", line 271, in _internal_call response.raise_for_status() File "/home/joachim/Jarvis/venv/lib/python3.12/site-packages/requests/models.py", line 1026, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://api.spotify.com/v1/users/test/playlists During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/joachim/Jarvis/spotifyv2.py", line 33, in <module> playlist=sp.user_playlist_create('test',name=playlistname, public=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/joachim/Jarvis/venv/lib/python3.12/site-packages/spotipy/client.py", line 872, in user_playlist_create return self._post(f"users/{user}/playlists", payload=data) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/joachim/Jarvis/venv/lib/python3.12/site-packages/spotipy/client.py", line 326, in _post return self._internal_call("POST", url, payload, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/joachim/Jarvis/venv/lib/python3.12/site-packages/spotipy/client.py", line 291, in _internal_call raise SpotifyException( spotipy.exceptions.SpotifyException: http status: 403, code: -1 - https://api.spotify.com/v1/users/test/playlists: You cannot create a playlist for another user, reason: None
用户提供的代码如下:
import pprint import sys import spotipy # la librairie pour manipuler l'api spotify import spotipy.util as util import simplejson as json #pour manipuler les réponses json import time #pour créer une playlist horodatée from datetime import datetime from random import shuffle #pour attribuer un classement aléatoire aux morceaux username="00000" clientId= "0000" clientSecret="0000" scope = 'playlist-modify-public playlist-modify-private' if len(sys.argv) > 1: username = sys.argv[1] else: print("Usage: %s username" % (sys.argv[0],)) sys.exit() token = util.prompt_for_user_token(username,scope,client_id=clientId,client_secret=clientSecret,redirect_uri='http://127.0.0.1:8888/callback') search="quicksand" timestr = time.strftime("%Y%m%d") playlistname=timestr+"_related_"+search if token: sp = spotipy.Spotify(auth=token) sp.trace = False # Création de la playlist playlist=sp.user_playlist_create('test',name=playlistname, public=False) playlist_id= str(playlist['id']) else: print("Can't get token for", username)
错误原因
- 代码调用
user_playlist_create时硬编码了目标用户为test,但你通过命令行传入的是自己的个人账户,Spotify API不允许使用当前账户的令牌为其他用户创建播放列表,因此返回403禁止访问错误。 - 你已经通过
sys.argv获取了正确的目标用户名,但没有在创建播放列表时使用这个变量,而是误用了固定值test。
解决办法
修改创建播放列表的代码行,将硬编码的'test'替换为已获取的username变量:
修改前
playlist=sp.user_playlist_create('test',name=playlistname, public=False)
修改后
playlist=sp.user_playlist_create(username,name=playlistname, public=False)
另外,你申请的playlist-modify-public和playlist-modify-private权限已经满足创建私有/公开播放列表的需求,无需调整scope。
内容的提问来源于stack exchange,提问作者Joachim serv
相关产品推荐
相关产品推荐

