![](/img/trans.png)
[英]Getting 401 error while making a PUT request to the Spotify API
[英]Getting "Request failed with status code 401" error when trying to create a playlist in Spotify using their API
这是我正在使用的代码:
await axios.post(arg.url,
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${SPYauthToken}`
},
params: {name: "playlist"},
}
)
我之前已经提供了对所有必要范围的访问权限并获得了相应的身份验证令牌,但是当我尝试使用我提供的代码创建播放列表时,我收到一条错误消息“请求失败,状态代码为 401”。
当我尝试在 Spotify 自己的 API 站点中创建播放列表时,它也不起作用: https : //developer.spotify.com/console/post-playlists/使用他们自己的示例单击“尝试”后,没有任何反应,也没有播放列表是为我创造的。
我做错了什么,还是Spotify必须解决的问题?
谢谢!
没试过,但你可以做以下
// request data object
const data = {
name: "playlist"
};
// set the headers
const config = {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${SPYauthToken}`
}
};
await axios.post(arg.url, data, config);
// maybe you need to stringify data so
await axios.post(arg.url, JSON.stringify(data), config);
await axios.post(arg.url,
{
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${SPYauthToken}`
},
params: {name: "playlist"},
}
)
您没有在授权周围使用引号
我使用 fetch 修复了它:
const data = { name: 'playlist' };
fetch(arg.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
"Authorization": `Bearer ${SPYauthToken}`
},
body: JSON.stringify(data),
})
.then((response) => response.json())
完美运行。 Axios 似乎不能使用 POST 工作,至少不像预期的那样,所以我可能会停止使用它。
感谢所有回答的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.