我在使用Spotify API向用户播放列表添加曲目时遇到了一个非常常见的错误 . 在先前的获取方法中,我已经获得了用户playlistId,并且现在尝试使用该播放列表ID将曲目发布到该播放列表上 . 我已经按照文档但显然遗漏了一些东西,这里是代码:
` //userUd, playlistId, currentUserAccessToken, trackURIs are all defined
fetch(`https://api.spotify.com/v1/users/${userId}/playlists/${playlistId}/tracks`, {
headers: {
'Authorization': 'Bearer ' + currentUserAccessToken
},
contentType: 'application/json',
method: 'POST',
body: JSON.stringify({
"uris": `[${trackURIs}]`
})
}).then(success => {
return success;
}).catch(err => {
console.log('here is your error', err);
})`
我做了GET请求来授权用户 - 包含创建公共播放列表的范围,在不同的代码块中工作,这是:
`let scopes ='playlist-modify-public';
window.location.replace( https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token
);`
万分感谢!
1 回答
JSON数组需要在其中的每个元素周围引用 . 因此应引用每个曲目,如下所示:
{"uris": ["spotify:track:4iV5W9uYEdYUVa79Axb7Rh", "spotify:track:1301WleyT98MSxVHPZCA6M"]}
我想你正在提供一个带有无效数组的对象名称uris .
并且,请注意,您也可以将播放列表项作为查询参数传递(而不是在正文中),而不使用json . https://developer.spotify.com/web-api/add-tracks-to-playlist/