Spotify:创建播放列表

Spotify: Create Playlist

扩展 https://github.com/spotify/web-api-auth-examples - authorization_code 文件夹中的代码。

它注销 access_token 没问题,但随后在 post 请求时挂起:

app.get('/createPlaylist', function(req, res) {

  console.log('access_token=' + access_token)

  request.post(
      'https://api.spotify.com/v1/users/' + user_id + '/playlists',
      {
        headers: {
          'Authorization' : access_token,
          'Content-Type': 'application/json'
        },
        json: {
          "collaborative": false,
          "description": null,
//        "external_urls": {
//          "spotify": "http://open.spotify.com/user/thelinmichael/playlist/7d2D2S200NyUE5KYs80PwO"
//        },
//    "followers": {
//      "href": null,
//          "total": 0
//    },
//        "href": "https://api.spotify.com/v1/users/thelinmichael/playlists/7d2D2S200NyUE5KYs80PwO",
//        "id": "7d2D2S200NyUE5KYs80PwO",
          "href": null,
          "id": null,
          "images": [],
          "name": "A Generated Playlist",
          "owner": {
            "external_urls": {
              "spotify": "http://open.spotify.com/user/1150816110"
            },
            "href": "https://api.spotify.com/v1/users/1150816110",
            "id": "1150816110",
            "type": "user",
            "uri": "spotify:user:1150816110"
          },
          "public": true,
//          "snapshot_id": "s0o3TSuYnRLl2jch+oA4OEbKwq/fNxhGBkSPnvhZdmWjNV0q3uCAWuGIhEx8SHIx",
          "tracks": {
            "href": "https://api.spotify.com/v1/users/thelinmichael/playlists/7d2D2S200NyUE5KYs80PwO/tracks",
//            "href": "https://api.spotify.com/v1/users/kb8mc65qdvz4lz0gdk0i4ztp3/playlist/46RFjEgbskxglR8rVsu38x/tracks",
            "items": [],
            "limit": 100,
            "next": null,
            "offset": 0,
            "previous": null,
            "total": 0
          },
          "type": "playlist",
//          "uri": "spotify:user:thelinmichael:playlist:7d2D2S200NyUE5KYs80PwO"
        }
      },
      function (error, response, body) {
        if (!error && response.statusCode == 200) {
          console.log(body);
        }
      }
  );
});

知道哪里出了问题吗?

'It looks like your post is mostly code; please add some more details.'

Whosebug 有哪些细节?我只想知道为什么 POST 请求挂起。

根据 Kevin 的规范修改了我的代码...不敢相信我误读了输入的输出...但它仍然挂起...节点版本 v12.14.1

app.get('/createPlaylist', function (req, res) {

  console.log('access_token=' + access_token)

  request.post(
      'https://api.spotify.com/v1/users/' + user_id + '/playlists',
      {
        headers: {
          'Authorization': access_token,
          'Content-Type': 'application/json'
        },
        json: {
          name: 'A generated playlist',
          public: true,
          collaborative: false,
          description: 'Generated at ' + Date.now(),
        }
      },
      function (error, response, body) {
        if (!error && response.statusCode === 200) {
          console.log(body);
        }
      }
  );
});

比较您的代码 to the documentation,看起来代码正在发送 JSON 输出作为示例中的输入。

要创建播放列表,there are only 4 body parameters:

  1. name - 字符串 - 必需
  2. public - 布尔值 - 可选
  3. collaborative - 布尔值 - 可选
  4. description - 字符串 - 可选。

我不知道所有其他信息是从哪里来的,但 API 可能会挂断所有这些信息。 还要确保您 the right scopes 具有为用户创建播放列表的权限。

请求看起来会被精简:

request.post(
      'https://api.spotify.com/v1/users/' + user_id + '/playlists',
      {
        headers: {
          'Authorization' : access_token,
          'Content-Type': 'application/json'
        },
        json: {
           name: 'A generated playlist',
           public: true,
           description: 'A playlist generated by JS'
        }
      }
...