我正在按照js-code运行。但我不断从Spotify网页上得到一个错误(说有某种错误)。credentials是由client_id和secret组成的base64编码字符串。
curl-command工作得很好:
curl -X "POST" -H "Authorization: Basic *credentials*" -d grant_type=client_credentials https://accounts.spotify.com/api/token
JS代码不能正常工作。
我想这很容易,但是我在js方面不是很好(对不起!)。
null
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);
});
null
正如Spotify授权指南中所指定的,主体必须是application/x-www-form-urlencoded
,因此只需添加以下标题:
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);
});