使用Spotipy将曲目添加到播放列表时出现多个错误

时间:2020-05-29 21:14:24

标签: python authentication spotify spotipy

我正在尝试将曲目添加到我的一个Spotify播放列表中,并且我很确定我拥有Spotipy文档中所述的所有代码完全正确:

username = '*myusername*'
scope = 'playlist-modify-public'

playlist_id = '*myplaylistid*'
track_ids = *array of track ids*

token = util.prompt_for_user_token(username,
                           scope,
                           client_id='*myclientid*',
                           client_secret='*mysecretclientid*',
                           redirect_uri='http://localhost:8888/callback/')
spotify = spotipy.Spotify(auth=token)

results = spotify.user_playlist_add_tracks(username, playlist_id, track_ids)

但是,这些是以下错误,无论我尝试什么,都会收到两个错误:

HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/users/*myusername*/playlists/*myplaylist*/tracks

During handling of the above exception, another exception occurred:

SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/users/*myusername*/playlists/*myplaylist*/tracks:
 Invalid track uri: spotify:track:*trackid*

它指定了无效的曲目uri,但是我通过在Spotify中搜索uri对我列表中的每个曲目进行了测试,

我无济于事的解决方案:
1.在播放列表和曲目列表的ID和URI之间更改
2.使用OAuth进行身份验证
3.使用不同的播放列表和曲目
4.使用不同的redirect_uri

5个示例轨道URI供参考:

spotify:track:1rdHsnsGmleo6MRctkkFmm?si=7R0IKQ9xTgiwfLAJO7eFCw
spotify:track:70CMnzQ3FjMmUk5NPdQJBe?si=qL_WwgWVRTaSZ2oOBg2eCA
spotify:track:6bbx7nYlixYuElKMbYCzMm?si=Wu64S-obRaOOh3mFP3zWwA
spotify:track:6DZNQKNUskiWVSXs3cQPk3?si=SIW3hBU1SiWd_h1gpXwijg
spotify:track:2FMPIU8FdP9kCi5kUCSGnE?si=jtJOkQhsSF6GoD3otgtV3A

将感谢您的帮助!!谢谢

1 个答案:

答案 0 :(得分:1)

轨道URI不应包含?si=,而只能包含之前的内容。参见https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids

您可以修复代码以仅保留第一部分:

track_uris = [uri.split("?si=")[0] for uri in track_uris]