thelinmichael / spotify-web-api-node

A Node.js wrapper for Spotify's Web API.
http://thelinmichael.github.io/spotify-web-api-node/
MIT License
3.1k stars 499 forks source link

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

Open glenbnguyen opened 6 years ago

glenbnguyen commented 6 years ago

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 track 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:

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:


  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);
    });
seandex commented 6 years ago

they blocked it like a month ago. either api version changed or token only valid through api.spotify.com (not tested)