- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 !
Solved! Go to Solution.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page