简体   繁体   English

Spotify API发布请求将曲目添加到播放列表-错误400错误的请求

[英]Spotify API Post Request Add Tracks To Playlist - ERROR 400 BAD REQUEST

Im having what seems to be a very common error when working on the Spotify API adding tracks to a users playlist. 我在使用Spotify API将曲目添加到用户播放列表时遇到了一个非常常见的错误。 In a previous fetch method I have obtained the users playlistId and am now trying to post tracks onto that playlist with that playlistId. 在以前的提取方法中,我已经获得了用户playlistId,现在正尝试使用该playlistId将曲目发布到该播放列表上。 I have followed the documentation but am obviously missing something, here is code: 我遵循了文档,但显然缺少一些内容,这是代码:

    `        //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);
          })`

I did the GET request to authorize user - included scope for creating public playlist, in different code block which is working, here's that: 我执行了GET请求来授权用户-包括创建公共播放列表的范围,在可以正常工作的其他代码块中,这是:

`let scopes = 'playlist-modify-public'; `let scopes ='playlist-modify-public';

window.location.replace( https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token );` window.location.replace( https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token { https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token } https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token scope https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token ${ https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token } https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token redirect_uri https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token ${ https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token } https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token response_type https://accounts.spotify.com/authorize?client_id=${clientID}&scope=${scopes}&redirect_uri=${redirectURI}&response_type=token token);`

Thanks a ton! 万分感谢!

The JSON array needs quotes around each element in it. JSON数组中的每个元素都需要用引号引起来。 So each track should be quoted, like so: 因此,应引用每条音轨,如下所示:

{"uris": ["spotify:track:4iV5W9uYEdYUVa79Axb7Rh", "spotify:track:1301WleyT98MSxVHPZCA6M"]}

I think you're supplying an object names uris with an invalid array. 我认为您提供的对象名称uris具有无效的数组。

And, note you can pass the playlist items as query params too (instead of in the body), without using json. 并且,请注意,您也可以将播放列表项作为查询参数传递(而不是在正文中),而无需使用json。 https://developer.spotify.com/web-api/add-tracks-to-playlist/ https://developer.spotify.com/web-api/add-tracks-to-playlist/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM