【问题标题】:Issue with POST Request To Spotify API对 Spotify API 的 POST 请求问题
【发布时间】:2018-02-04 02:17:00
【问题描述】:

我正在开发一个网络应用程序,您可以在其中搜索 Spotify 库,将歌曲添加到播放列表,然后将该播放列表添加到您的 Spotify 帐户。除了通过对 Spotify API 的 POST 请求保存播放列表之外,一切似乎都在工作。它给了我一个“400 Bad Request”错误。我可能只是错过了一些小东西,有什么想法吗?请求方式:

async savePlaylist(name, trackURIs) {
        if(accessToken === undefined) {
            this.getAccessToken();
        }
        if (name === 'New Playlist' || name === undefined || trackURIs === undefined) {
            return;
        } else {
            let userAccessToken = this.getAccessToken();
            let headers = {Authorization: `Bearer ${userAccessToken}`};
            let userId = await this.findUserId();
            let playlistID;
            fetch(`https://api.spotify.com/v1/users/${userId}/playlists`, {
                method: 'POST',
                headers: {
                    Authorization: `Bearer ${accessToken}`,
                    "Content-Type": 'application/json'
                },
                body: {
                    name: name
                }
            }).then(response => {return response.json()}
            ).then(playlist => {
                playlistID = playlist.id;
            });
        }
    },

如果您需要查看我正在开发的分支的 repo,可以找到 here

编辑:可能需要API Endpoint Documentation

【问题讨论】:

    标签: javascript reactjs spotify


    【解决方案1】:

    我注意到您的代码有两点:

    授权:Bearer ${accessToken}

    accessToken 似乎没有在该代码块中的任何地方定义,而是我看到userAccessToken

    错误的第二个也是最可能的原因是您的请求正文应该是字符串(JSON 字符串化)而不是对象

    this answers shows a good example

    PS:我不知道为什么有人投了反对票并且没有给出理由,我讨厌这种情况发生

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多