【发布时间】:2021-03-22 01:41:35
【问题描述】:
我正在尝试使用 Discord 的 API 创建登录过程。根据文档,我假设使用回调 URL 上返回的 code 进行另一个调用以接收访问令牌。我已经按照here 的说明进行操作。但是,我在拨打电话时不断收到400 错误。
对我需要更正的内容有什么帮助吗?
let options = {
method: 'POST',
headers: {
'Authorization': 'Basic ' + discordInfo.client_id + ":" + discordInfo.client_secret,
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
'client_id': discordInfo.client_id,
'client_secret': discordInfo.client_secret,
'grant_type': 'authorization_code',
'code': req.query.code,
'redirect_uri': discordInfo.callbackUrl,
'scope': 'identify email'
}
}
let discord_data = await fetch('https://discord.com/api/oauth2/token', options).then((response) => {
if (response.status >= 400) {
throw new Error("Bad response from server");
}
return response.json();
}).then((response) => {
console.log('Discord Response', response);
}).catch((error) => {
console.log(error);
});
【问题讨论】:
标签: node.js discord fetch discord.js http-status-code-400