I'm running following js-code. But I keep getting an error from Spotify Webpage (saying there was some sort of error). credentials is a base64 encoded string of client_id and secret.
The curl-command works just fine:
curl -X "POST" -H "Authorization: Basic *credentials*" -d grant_type=client_credentials https://accounts.spotify.com/api/token
The js-code doesn't work properly..
I guess it's pretty easy, but I'm not that good in js (sorry!).
fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {'Authorization': 'Basic *credentials*'},
body: 'grant_type=client_credentials'
})
.then(response => response.json())
.then(data => {
console.log(data);
});
As specified in the Spotify Authorization guide the body has to be application/x-www-form-urlencoded
so just add this header:
fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
'Authorization': 'Basic *credentials*',
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: 'grant_type=client_credentials'
})
.then(response => response.json())
.then(data => {
console.log(data);
});