【问题标题】:Discord OAuth2: 'Missing "code" in request'Discord OAuth2:'请求中缺少“代码”'
【发布时间】:2020-10-12 20:12:44
【问题描述】:

几个月前我写了一个 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.0 discord


    【解决方案1】:

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

    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。

    【讨论】:

    • 它不工作。它给出错误“invalid_grant”。
    猜你喜欢
    • 2021-04-12
    • 1970-01-01
    • 2012-08-29
    • 2021-12-13
    • 1970-01-01
    • 2020-12-05
    • 2019-05-02
    • 2021-07-21
    • 1970-01-01
    相关资源
    最近更新 更多