首页 > 解决方案 > 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();

标签: oauth-2.0discord

解决方案


看起来您已经获得了调用令牌端点并将代码交换为令牌所需的字段。您需要在表单正文中发送数据 - 如下所示:

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。


推荐阅读