Search code examples
node.jsspotify

Spotify: Create Playlist


Extending https://github.com/spotify/web-api-auth-examples - the code in the authorization_code folder.

It logs out access_token okay, but then hangs at the post request:

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

Any idea what is wrong?

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

What details StackOverflow? I just wanna know why the POST request hangs.

Modified my code to Kevin's specs ... can't believe I misread output for input ... but it still hangs ... node version 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);
        }
      }
  );
});

Solution

  • Comparing your code to the documentation, it looks like the code is sending the JSON output as the input in the example.

    To create a playlist, there are only 4 body parameters:

    1. name - string - required
    2. public - boolean - optional
    3. collaborative - boolean - optional
    4. description - string - optional.

    I don't know where all the other information is coming from but the API could be hanging up on all of it. Also be sure you have the right scopes involved with permission to create a playlist for a user.

    The request would look trimmed down:

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