question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting 404 errors when attempting to access a user's playlists

See original GitHub issue

I’m using Node version 5.5.0, and spotify-web-api-node version 2.3.0. I’m using the following function to get the playlists from my own Spotify account.

var SpotifyWebApi = require('spotify-web-api-node');
var clientId = '<client id>';
var clientSecret = '<client secret>';

var spotifyApi = new SpotifyWebApi({
    clientId: clientId,
    clientSecret: clientSecret
});

function getUserPlaylists(userId) {
    spotifyApi.clientCredentialsGrant()
        .then(function(data) {
            spotifyApi.setAccessToken(data.body['access_token']);
            spotifyApi.getUserPlaylists(userId)
                .then(function(data) {
                    console.log(data);
                }, function(err) {
                    console.log('Something went wrong!', err);
                });
        }, function(err) {
            console.log('Something went wrong when retrieving an access token', err);
        });
}

However, whenever I call this function with my own Spotify user ID (I signed up with Facebook), I get the following error: Something went wrong! { [WebapiError: Not found.] name: 'WebapiError', message: 'Not found.', statusCode: 404 }. Other API functions work still, but all of a sudden this one ceased to work even though I haven’t touched my server or the code for my server since it was working well. I checked the endpoints in the spotify-web-api-node source code and they seem to still be correct for getUserPlaylists.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
stfnlzvncommented, Jul 6, 2017

Ran into the same problem when using getPlaylistTracks(). Fixed it by passing playlist.owner.id rather than user.id

0reactions
jswnycommented, Jun 21, 2016

Once again, after having changed absolutely nothing on my production server, I’m getting the same error again. Testing my site on my local machine yields the same results. Upon further investigation, I’ve found the problem. After I grab the user’s playlists, I’m iterating over them like this:

spotifyApi.getUserPlaylists(userId)
    .then(function(data) {
        var playlists = data.body.items
        var playlistsArr = new Array()
        var count = 0
        playlists.forEach(function(playlist) {
            spotifyApi.getPlaylist(userId, playlist.id)
                .then(function(data) {
                    playlistsArr.push({
                        name: playlist.name,
                        id: playlist.id,
                        img: playlist.images[2].url,
                        followers: data.body.followers.total,
                        total_tracks: playlist.tracks.total
                    })
                    if (count == playlists.length - 1) {
                        callback(playlistsArr)
                    }
                    count++
                }, function(err) {
                    console.log('Something went wrong retrieving an individual playlist!', err)
                })
        })
    }, function(err) {
        console.log("Something went wrong retrieving the user's playlists!", err)
    })

However, the page would hang and never load, which was the reason I started noticing this problem. Turns out, some of the Spotify playlists that I followed were made public on my profile, thus they were pulled into what my code thought was an array of my playlists. Then, when I tried to use getPlaylist(userId, playlist.id) it would hang. That’s because those other playlists that I had followed were not mine, so when I looped through one of those, the whole script would hang. To fix this, I’ve made all of those secret but you could just as easily check each playlist for the property collaborative or something similar before looping through each one. That would explain the 404 errors as well because the API was trying to access a playlist that didn’t exist from my user ID. This turned out to be my incompetence, nothing wrong with any of the tools I was using. Hopefully this helps others to fix this mistake more easily. Thank you @JMPerez for the help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Find & Fix 404 Errors On Your Website
The most critical question is how do you fix 404 errors? The best way to fix a 404 error is to redirect the...
Read more >
How to Find and Fix 404 Errors
404 errors can ruin your user experience and kill your organic rankings. ... It means the page the viewer is trying to reach...
Read more >
How to Fix Error 404 Not Found on Your WordPress Site
The Error 404 Not Found status code indicates that the origin server did not find the target resource. Check out these common causes...
Read more >
How to Find and Fix a 404 Error on Your Website
A 404 error indicates that the requested page cannot be found. These can be extremely damaging to your brand and have a negative...
Read more >
Error 404 Not Found: everything you need to know
Refresh the page : Sometimes, simply refreshing the page can resolve the error. You can try pressing F5 on your keyboard or clicking...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found