1
votes

I am trying to do a simple example based on the code for the nodejs 'spotify-web-api-node': https://github.com/thelinmichael/spotify-web-api-node , Im trying to get the example working without having to prompt a user to sign in.

I have setup a spotify developer app so I can get a clientId and clientSecret, and I added the url for my local hosting:

enter image description here

My function gets called when a button is pressed, and tries to authenticate the account and then search for elvis's albums. I added the same url I put in the settings above as a redirectUri:

//popularify spotify api route
app.post('/popularifyRequest', async function (req, res) {
  console.log("/popularifyRequest req.body=",req.body)
  
  var SpotifyWebApi = require('spotify-web-api-node');
 
  // credentials are optional
  var spotifyApi = new SpotifyWebApi({
    clientId: 'f8```````````````````',
    clientSecret: 'b7``````````````````````',
    redirectUri: 'http://localhost:8080/popularify'
  });
 
  // Get Elvis' albums
  spotifyApi.getArtistAlbums('43ZHCT0cAZBISjO8DG9PnE').then(
    function(data) {
      res.send(data.body);
    },
    function(err) {
      res.send(err);
    }
  );

});

But when the function gets called; I get a 401 err:

name: "WebapiError", message: "Unauthorized", statusCode: 401}

did I authenticate my account wrong?

1

1 Answers

0
votes

After getting authorized (redirected to Shopify and redirected back to /popularify ) from Spotify you should use the returned token

// in app.get("/popularify", async function (req, res))

spotifyApi.authorizationCodeGrant(code).then(
  function(data) {


    spotifyApi.setAccessToken(data.body['access_token']);
    spotifyApi.setRefreshToken(data.body['refresh_token']);

spotifyApi.getArtistAlbums('43ZHCT0cAZBISjO8DG9PnE').then(
        function(data) {
          res.send(data.body);
        },
        function(err) {
          res.send(err);
        }
      );



  }).catch((err) {
    console.log('Something went wrong!', err);
  })
);