为什么在尝试创建播放列表时,Spotify会返回403错误?
我在尝试使用我的账户创建播放列表时遇到了这个错误。当我尝试用命令 python3 spotifyv2.py {user_name} 运行代码时,它给我这个错误:
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)
解决方案
将
playlist = sp.user_playlist_create('test', name=playlistname, public=False)
改为
playlist = sp.user_playlist_create(username, name=playlistname, public=False)
其中 username 应该是保存 sys.argv[1] 的变量。
并添加以下内容:
import sys
username = sys.argv[1]
Spotify的 API会在经过身份验证的用户与目标用户ID不匹配时拒绝创建播放列表。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。