Search code examples
javascriptjqueryajaxpostspotify

(Spotify Web API) Create New Playlist - POST request returning 'Error 403 (Forbidden)'


Here is the link to the Web API notes on how to create a new playlist. https://developer.spotify.com/web-api/create-playlist/

As far as I understand, the POST requests the url https://api.spotify.com/v1/users/{user_id}/playlists. This is requested while passing the access token and data. The content type of the data being 'application/json'.

For some reason this is failing and returning a Error 403 (Forbidden) in the console.

Anything I'm missing?

//(playlistName, userId, accessToken) are passed to this.

var urlString = 'https://api.spotify.com/v1/users/' + userId + '/playlists';

var jsonData = {
  "name": playlistName,
  "public": false
};

$.ajax({
  type: 'POST',
  url: urlString,
  data: jsonData,
  dataType: 'json',
  headers: {
    'Authorization': 'Bearer ' + accessToken
  },
  contentType: 'application/json',
  success: function(result) {
    console.log('Woo! :)');
  },
  error: function() {
    console.log('Error! :(');
  }
})


Solution

  • Having tried your example, I get a 401 unauthorized when filling in bogus data. So you are authorized, but the API really does not grant you the rights (403 forbidden).

    Please have a look at the authorization guide. I am pretty sure, your error is there. Especially have a look at scope. You might simply not grant enough power in the login. And therefore ending up with only public access, which does not include adding playlists.

    I cite form the API docs:

    To be able to create private playlists, the user must have granted the playlist-modify-private scope.

    https://developer.spotify.com/web-api/authorization-guide/