错误400错误请求Spotify API发布请求

时间:2018-03-07 04:06:23

标签: reactjs api spotify

我正在开发一个Web应用程序,用户可以搜索音乐,创建新的播放列表,然后将其保存到Spotify帐户。我目前停留在用户Spotify上创建一个新的播放列表。

    `        //userUd, playlistName, currentUserAccessToken are all defined 

fetch(`https://api.spotify.com/v1/users/${userId}/playlists`, {
              headers: {
                'Authorization': 'Bearer ' + currentUserAccessToken
              },
              contentType: 'application/json',
              method: 'POST',
              body: {
                "name": `${playlistName}`,
                "description": `You made this playlist with Jammming, a ReactJS web application built by Max Page`
              }
            }).then(playlist => {
              console.log(playlist);
              return playlist.json();
            }).catch(err => {
              console.log(err);
            })`

这里的目标是获取新的播放列表,任何人都知道为什么我收到400 Bad Request错误?我做了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 个答案:

答案 0 :(得分:1)

字符串化你的身体。这就是为我修复了同样的错误

fetch(`https://api.spotify.com/v1/users/${userId}/playlists`, {
          headers: {
            'Authorization': 'Bearer ' + currentUserAccessToken
          },
          contentType: 'application/json',
          method: 'POST',
          body: JSON.stringify({
            "name": `${playlistName}`,
            "description": `You made this playlist with Jammming, a ReactJS web application built by Max Page`
          })
        }).then(playlist => {
          console.log(playlist);
          return playlist.json();
        }).catch(err => {
          console.log(err);
        })`