创建播放列表spotify API

create playlist spotify API

本文关键字:API spotify 播放列表 创建      更新时间:2023-09-26

我正在尝试使用Spotify API创建一个播放列表,并收到以下错误:

{
  "error" : {
    "status" : 400,
    "message" : "Error parsing JSON."
  }
}

我遵循这个页面上的例子,说要使用以下curl请求

curl -X POST "https://api.spotify.com/v1/users/thelinmichael/playlists" -H "Authorization: Bearer {your access token}" -H "Content-Type: application/json" --data "{'"name'":'"A New Playlist'", '"public'":false}"   

我的代码是这样的

$.ajax({
   url: 'https://api.spotify.com/v1/users/{id}/playlists',
   method: "POST",
   data: {
     name: "test"
   },
   headers: {
     'Authorization': 'Bearer ' + access_token,
     'Content-Type': 'application/json'
   },
   success: function(response) {
     console.log(response);
   }
 });

谁能告诉我做错了什么?

原因是我没有发布有效的儿子。通过使用JSON.stringify,我能够解决这个问题。

JSON.stringify({name: "test", public: false})

完整代码如下:

$.ajax({
   url: 'https://api.spotify.com/v1/users/{id}/playlists',
   method: "POST",
   data: {
     name: JSON.stringify({name: "test", public: false})
   },
   headers: {
     'Authorization': 'Bearer ' + access_token,
     'Content-Type': 'application/json'
   },
   success: function(response) {
     console.log(response);
   }
 });