英文:
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需要解决的问题?
谢谢!
英文:
Here is the code that I am using:
await axios.post(arg.url,
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${SPYauthToken}`
},
params: {name: "playlist"},
}
)
I have already previously provided access to all the necessary scopes and gotten a corresponding authentication token, yet when I try to create a playlist with the code I provided, I get an error saying "Request failed with status code 401".
Nor does it work when I tried creating a playlist in the Spotify's own API site here: https://developer.spotify.com/console/post-playlists/ After clicking "try it" using their own example, nothing happens and no playlist is created for me.
Am I doing something royally wrong, or is it something that Spotify has to sort out?
Thanks!
答案1
得分: 2
没有尝试过,但你可以按照以下步骤操作:
// 请求数据对象
const data = {
name: "playlist"
};
// 设置头部信息
const config = {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${SPYauthToken}`
}
};
await axios.post(arg.url, data, config);
// 如果需要对数据进行字符串化
await axios.post(arg.url, JSON.stringify(data), config);
英文:
Didn't try it but you can do following
// 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);
答案2
得分: 0
"你没有在Authorization周围使用引号"
英文:
await axios.post(arg.url,
{
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${SPYauthToken}`
},
params: {name: "playlist"},
}
)
You didn't used Quotes around Authorization
答案3
得分: 0
我使用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时不起作用,至少不如预期,所以我可能会停止使用它。
感谢所有回答的人。
英文:
I fixed it using 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())
Works flawlessly. Axios doesn't seem to work using POST, at least not as expected, so I'll probably stop using it.
Thanks to everybody who answered.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论