【问题标题】:Discord OAuth returns missing "code in requestDiscord OAuth 在请求中返回缺少的“代码”
【发布时间】:2021-04-12 08:02:55
【问题描述】:

我正在尝试使用一个简单的示例从 Discord API 获取访问令牌。我有授权码,但我正在努力将其换成访问令牌。

到目前为止我的代码:

const code = **my authorization code**;
const CLIENT_ID = **my client id**;
const CLIENT_SECRET = **my client secret**;
const creds = btoa(`${CLIENT_ID}:${CLIENT_SECRET}`);
fetch(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}&redirect_uri=${redirect}`,
  {
    method: "POST",
    headers: {
      Authorization: `Basic ${creds}`,
  },
})
  .then((res) => res.json())
  .then((body) => console.log(body));

但是,当我运行此代码时,我收到以下错误:

{
   error: 'invalid_request',
   error_description: 'Missing "code" in request.'
}

【问题讨论】:

    标签: node.js oauth-2.0 oauth discord discord.js


    【解决方案1】:

    我知道如何解决这个问题。所以 Discord API 已经更新,您不能再在 URL 中指定代码、授权类型和重定向 URI。您需要将其从 URL 中删除并放入正文中。

    这应该可以解决它:https://sourceb.in/BGWsbUzope

    【讨论】:

    • 如果我这样做,我只会得到“invalid_grant”错误。 “{错误:'invalid_grant'}”
    • 哦,我的错,不要使用 JSON 表示法,而是使用 querystring npm 模块。
    • 那是哪个模块?我从来没有听说过。
    • 在 NPM 上称为querystringnpmjs.com/package/querystring 使用它而不是 JSON。所以不是 JSON.stringify,而是 querystring.stringify。
    • 您能否将完整的解决方案添加到答案中,以便遇到此问题的每个人(如我)都有机会解决它?
    猜你喜欢
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2017-09-20
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    • 2019-07-11
    相关资源
    最近更新 更多