Home » Javascript » Spotify API Node.js Scope WebAPI Bad Request/Unauthorized Error

Spotify API Node.js Scope WebAPI Bad Request/Unauthorized Error

Posted by: admin August 19, 2018 Leave a comment

Questions:

I’ve been trying my best to figure this out but I just can’t understand what I’m doing wrong. I am fairly new to programming and would appreciate any attempts to help me learn in the long run.

My complete code currently can fetch the playlistID track information from the spotifyApi.getPlaylist method. I am able to extract all the necessary data I need from the playlist.

My problem is I can’t use the spotifyApi.removeTracksFromPlaylist or spotifyApi.addTracksToPlaylist because it returns WebAPI errors.

So it’s clear there’s something wrong with the way I’m implementing the scope.

I get this output error:

~/Desktop/ExSpotify/web-api-auth-examples-master/authorization_code$ node spotify-web-api.js 
https://accounts.spotify.com/authorize?client_id=17e4f45ce72c4a09a9e0b5a6374560bb&response_type=code&redirect_uri=https://localhost:8888/callback/&scope=playlist-modify-public%20playlist-modify-private&state=some-state-of-my-choice

Something went wrong! { [WebapiError: Unauthorized] name: 'WebapiError', message: 'Unauthorized', statusCode: 401 }

Could not refresh access token { [WebapiError: Bad Request] name: 'WebapiError', message: 'Bad Request', statusCode: 400 }

Something went wrong! { [WebapiError: Bad Request] name: 'WebapiError', message: 'Bad Request', statusCode: 400 }

My code so far is:

  var SpotifyWebApi = require('spotify-web-api-node');  

  var spotifyApi = new SpotifyWebApi({

  clientId: '17e4f45ce72c4a09a9e0b5a6374560bb',
  clientSecret: 'myClientSecret',  
  redirectUri: 'https://localhost:8888/callback/',

 });

 // SCOPE PROBLEM!!!!

 var scopes = ['playlist-modify-public', 'playlist-modify-private'],
 redirectUri = 'https://localhost:8888/callback/',
 clientId = '17e4f45ce72c4a09a9e0b5a6374560bb',
 state = 'some-state-of-my-choice';

var authorizeURL = spotifyApi.createAuthorizeURL(scopes, state);
console.log(authorizeURL);

var code = 'AQCOuessccKa-p3T6mlFyQB2GhaNjYmg--0bz6d0EuPiMEb-IxY8W_hoDlWZXdvXhYTwRgYaJeQ_7RtsVBYTio8Kw0qLnRNDTo7GjIO8vqBRWmSNktvt7ivY_yTSCKci axy7th1RZUm8xfYKA-6GfqjzQoGSwfPR0nxEAnuJELaOE0N7vNgqc4tzD8oTj-65C-bgOtcDyWqUia2mjnOQIsVJASFY9vJWEnwBJh2RZSE6Bg52hcHcjKyFu43CGQuXPwl5YFRVbkm9yYYrTuwgbu64Cd1eoHhR9kNs';

// Retrieve an access token and a refresh token
spotifyApi.authorizationCodeGrant(code).then(
  function(data) {
    console.log('The token expires in ' + data.body['expires_in']);
    console.log('The access token is ' + data.body['access_token']);
    console.log('The refresh token is ' + data.body['refresh_token']);

    // Set the access token on the API object to use it in later calls
    spotifyApi.setAccessToken(data.body['access_token']);
    spotifyApi.setRefreshToken(data.body['refresh_token']);
  },
  function(err) {
    console.log('Something went wrong!', err);
  }
);

  // clientId, clientSecret and refreshToken has been set on the api object previous to this call.
  spotifyApi.refreshAccessToken().then(
    function(data) {
      console.log('The access token has been refreshed!');

      // Save the access token so that it's used in future calls
      spotifyApi.setAccessToken(data.body['access_token']);
    },
    function(err) {
      console.log('Could not refresh access token', err);
    }
  );


  // Spotify user info
  var playlistId = '5UorUzUCb4ZUQzR8XickDQ';
  var userName = 'glenosis'; 

    //First track URI in playlist
    var tracks = [{ uri : "spotify:track:5pvNOAAlLVYmRTZa0MUouA" }];    

    spotifyApi.removeTracksFromPlaylist(userName, playlistId, tracks) 
    .then(function(data) {
      console.log('Tracks removed from playlist!');                   
    }, function(err) {
      console.log('Something went wrong!', err);
    });
Answers: