我正在使用 axios 和express 在 JavaScript 中编写一个基本程序,以了解有关 OAuth2 如何工作的更多信息,这时我的客户突然给出了错误。
错误:400(错误请求)
这是代码中出错的部分:
//^^^^^^^^^^
// defining web server
const formData = new url.URLSearchParams({
client_id: webid,
client_secret: websecret,
grant_type: 'authorization_code',
code: code.toString(),
redirect_uri: redirect_uri,
});
const oauthInfo = await axios.post('https://discord.com/api/v10/oauth2/token',
formData, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
});
// below is starting everything
formData 必须是 String 类型,而不是 Object。
const oauthInfo = await axios({
method: 'POST',
url: 'https://discordapp.com/api/oauth2/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: new URLSearchParams({
client_id: webid,
client_secret: websecret,
grant_type: 'authorization_code',
code: code,
redirect_uri: redirect_uri,
}).toString();
});