【问题标题】:Discord OAUTH error: 'unsupported_grant_type',Discord OAUTH 错误:'unsupported_grant_type',
【发布时间】:2021-12-30 03:07:25
【问题描述】:

我正在尝试在我的网站上实现 Discord-Login。不幸的是,我收到了不和谐的错误。

有人看到我的代码有什么问题吗?

仅供参考:我已经从 discord 中取回了代码。所以我的第一个请求运行良好。

我想对数据做什么:

我想获取 user_ID,以便在我的 Discord 服务器上向用户添加组。

错误:

error: 'unsupported_grant_type',
error_description: 'Grant type None is not supported'

代码:

router.get('/account-settings/connections/discord/callback', (async (req, res) => {

  const code = await req.query.code;
  const creds = btoa(`${CLIENT_ID}:${CLIENT_SECRET}`);

  fetch(`https://discordapp.com/api/oauth2/token`, {
          method: "POST",
          headers: {
              Authorization: `Basic ${creds}`,
          },
          body: querystring.stringify({
              grant_type: 'authorization_code',
              code: code,
              redirect_uri: dis_redirect
          }),
      })
      .then((res) => res.json())
      .then((body) => console.log(body));
})

提前非常感谢您!!!

最好的问候, 乔希

【问题讨论】:

  • 您可能错过了:Content-Type: application/x-www-form-urlencoded,但我不确定! await req.query.code 也是可疑的。它和你认为的一样吗?
  • 我必须把它放在哪里?当用户将他的个人资料授权给我的应用程序时,req.query.code 包含我从不和谐 oauth 获得的代码。所以是的,这应该是正确的:)

标签: javascript node.js oauth discord


【解决方案1】:

其他有此问题的人都可以。我能够通过解析正文中的所有信息并在标题中设置内容类型来解决此问题。这是一个工作代码:

let params = new URLSearchParams();
  params.append('client_id', CLIENT_ID);
  params.append('client_secret', CLIENT_SECRET);
  params.append('grant_type', 'authorization_code');
  params.append('code', code);
  params.append('redirect_uri', dis_redirect);

  fetch('https://discord.com/api/oauth2/token', {
    method: 'post',
    body: params,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' },
  }).then(r => r.json()).then(async Response => {
    

在代码变量中,我已经存储了请求查询,我正在从 TwithOauth 返回(在用户自动化之后)。 您将从 Discord 应用程序中获得的客户端 ID 和客户端密码。 dis_redirect 病毒包含我的重定向链接,它只是我的链接的空白字符串,我已经设置好获取路由器。

什么都不需要了。

您将收到一个 JSON 响应,您可以使用它。如果您想获取用户数据(如我),则以下代码就是您要搜索的内容:

let refresh_token = Response.refresh_token;
    let accessToken = Response.access_token;

    var site = await fetch("https://discord.com/api/v9/users/@me", {
        method: 'GET',
        headers: {'Authorization': `Bearer ${accessToken}`}
    });
    var response = await site.json();
    console.log(response);
  });

此代码您将直接放置在新打开的异步函数中。你有它。您将需要所有用户数据。

我希望我能够帮助别人。如果您有任何问题,请告诉我!

最好的问候, 乔希

【讨论】:

    猜你喜欢
    • 2019-04-22
    • 2020-06-26
    • 1970-01-01
    • 2021-10-02
    • 2018-10-07
    • 2017-12-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    相关资源
    最近更新 更多