首页 > 解决方案 > Spotify:创建播放列表

问题描述

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

它可以注销 access_token ,但随后在发布请求时挂起:

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

知道有什么问题吗?

'看起来你的帖子主要是代码; 请添加更多细节。

StackOverflow 有哪些细节?我只想知道为什么 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);
        }
      }
  );
});

标签: node.jsspotify

解决方案


将您的代码与文档进行比较,看起来代码将 JSON 输出作为示例中的输入发送。

要创建一个播放列表,只有 4 个 body 参数

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

我不知道所有其他信息来自哪里,但 API 可能会挂断所有这些信息。还要确保您具有为用户创建播放列表的权限所涉及的正确范围。

该请求看起来会被削减:

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'
        }
      }
...

推荐阅读