[英]How to POST to Spotify API token endpoint with JavaScript?
我正在运行以下 js 代码。 但是我不断收到来自 Spotify 网页的错误消息(说有某种错误)。 凭证是由 client_id 和 secret 组成的 base64 编码字符串。
curl 命令工作得很好:
curl -X "POST" -H "Authorization: Basic *credentials*" -d grant_type=client_credentials https://accounts.spotify.com/api/token
js 代码不能正常工作..
我想这很容易,但我不太擅长 js(对不起!)。
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); });
按照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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.