Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

Server Error when trying to recover last listened track /v1/me/player/recently-played

Solved!

Server Error when trying to recover last listened track /v1/me/player/recently-played

Plan

Premium

Country

France

Device

PC

Operating System

Windows 11

 

 

Hi everyone !

I would like to ask for your help with using the Spotify API.
I'm trying to add the latest music I listened to on Spotify to my website, but when I try to retrieve this music, I get a 500 error, which means "Server Error".
I thought it might be a problem with my code or the authorization I'm using, so I switched from "Authorization code" to "Implicit Grant," but the problem remains the same. Can someone explain to me what could be wrong, or should I look for the error myself?
Here's my code:

 

 

 

let clientId = 'myClientId';
let clientSecret = 'myClientSecret';
let redirectUri = 'http://localhost:3000/api/spotify/callback';
let state = generateRandomString(16);
let scope = 'user-read-recently-played';

router.get('/spotify/', (req, res) => {
    try {
        var url = 'https://accounts.spotify.com/authorize';
            url += '?response_type=token';
            url += '&client_id=' + encodeURIComponent(clientId);
            url += '&scope=' + encodeURIComponent(scope);
            url += '&redirect_uri=' + encodeURIComponent(redirectUri);
            url += '&state=' + encodeURIComponent(state);
        
        res.redirect(url);
    }
    catch (err) {
        console.log();
    }
})

router.get('/spotify/callback', (req, res) => {
    try {
        var url = 'https://accounts.spotify.com/api/token';
        url += '?grant_type=client_credentials';
        url += '&code=' + encodeURIComponent(req.query.code);
        url += '&redirect_uri=' + encodeURIComponent(redirectUri);
        url += '&client_id=' + encodeURIComponent(clientId);
        url += '&client_secret=' + encodeURIComponent(clientSecret);
        url += '&scope=' + encodeURIComponent(scope);

        axios.post(url)
        .then(function (response) {
            var url = 'https://api.spotify.com/v1/me/player/recently-played';
            url += '?limit=1';

            axios.get(url, {
                headers: {
                    'Authorization': 'Bearer ' + response.data.access_token,
                    
                }
            })
            .then(function (response) {
                console.log(response);
                res.json(response);
            })
        })
    }
    catch (err) {
        console.log(err);
    }
})

 

 

 

 

And here's the error I'm receiving:

 

 

 

data: { error: { status: 500, message: 'Server error.' } }

 

 

 

 

If someone can help me to resolve this problem it would make my day 🙂 

Thank you and have a wonderful week ! 

 

 

Reply

Accepted Solutions
Marked as solution

Since [Client Credentials] does not include authorization, only endpoints that do not access user information can be accessed.

The page of the Get Recently Played Tracks endpoint access user data, and it mentions Authorization scopes.

So implementing the Authorization Code Flow is required to let your app work.

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

View solution in original post

1 Reply
Marked as solution

Since [Client Credentials] does not include authorization, only endpoints that do not access user information can be accessed.

The page of the Get Recently Played Tracks endpoint access user data, and it mentions Authorization scopes.

So implementing the Authorization Code Flow is required to let your app work.

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

Suggested posts