【发布时间】:2018-08-15 23:13:48
【问题描述】:
在使用 Spotify API 向用户播放列表添加曲目时,我遇到了一个似乎很常见的错误。在之前的 fetch 方法中,我获得了用户 playlistId,现在正尝试使用该 playlistId 将曲目发布到该播放列表中。我遵循了文档,但显然遗漏了一些东西,这里是代码:
` //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);`
非常感谢!
【问题讨论】:
-
trackURIs 是否正确定义为逗号分隔的 spotify URI 列表?
-
是的,它是一个字符串数组 - 字符串是所选曲目 uri
-
示例喜欢这个
{"uris":"[spotify:track:4omURIn6jRJ77sHxzSxor5,spotify:track:0mm5kY0FbqycYqXUvUBUn0]"}