oauth-2.0 - Discord OAuth2:'请求中缺少“代码”'
问题描述
几个月前,我写了一个 Discord OAuth2 验证码,它奏效了。但现在它不起作用。Discord 响应“请求中缺少“代码””。我看了看,我的请求发送了代码。我该如何解决这个问题?
采取的代码access_token
:
const response = await fetch(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}&redirect_uri=${loginredirect}`,
{
method: 'POST',
headers: {
Authorization: `Basic ${creds}`,
}
});
const json = await response.json();
解决方案
看起来您已经获得了调用令牌端点并将代码交换为令牌所需的字段。您需要在表单正文中发送数据 - 如下所示:
const response = await fetch(`https://discordapp.com/api/oauth2/token`,
{
method: 'POST',
headers: {
Authorization: `Basic ${creds}`,
},
body: JSON.stringify({
grant_type: 'authorization_code',
code,
redirect_uri: loginredirect,
}),
});
const json = await response.json();
我还会考虑使用 OAuth 安全库(例如openid-client)为您进行更多安全检查,而不是使用原始提取 API。
推荐阅读
- node.js - 将数据从反应组件传递到代理(节点服务器)
- python-3.x - 使用 argparse 从脚本调用函数而不使用子进程
- wordpress - 我应该包括哪个 fontawesome 文件以及为什么其他文件不起作用
- python - 在我的班级中导入模块的正确方法是什么
- python - 如何使用python烧瓶从维基百科页面获取表格数据
- reactjs - 为什么没有定义“handlePost”?
- environment-variables - 如何在开始管道代码之前使用共享库在 jenkinsfile 中注入环境变量?
- c# - Rabbit Mq,在发布者之前启动消费者
- android - MaterialTapTargetPrompt:让跳过按钮跳过教程的方法
- node.js - 与环回角色映射中的 principalId 混淆