不和谐的oauth中的错误请求

Posted

技术标签:

【中文标题】不和谐的oauth中的错误请求【英文标题】:Bad request in discord oauth 【发布时间】:2021-12-02 10:53:09 【问题描述】:

我正在创建一个 Discord 登录,但是当我被重定向时,我得到了错误。我在 Discord OAuth 页面上按授权后发生错误 "status":"ERROR","error":"Response code 400 (Bad Request)" 这是我的 Discord.js 代码

const express = require('express');

const router = express.Router();
const fetch = require('got');
const btoa = require('btoa');
const  catchAsync  = require('../utils');

const CLIENT_ID = '#';
const CLIENT_SECRET = '#';
const redirect = encodeURIComponent('https://ezapplications.live/api/discord/callback');

router.get('/callback', catchAsync(async (req, res) => 
  if (!req.query.code) throw new Error('NoCodeProvided');
const response = await fetch(`https://discordapp.com/api/oauth2/authorize?client_id=$CLIENT_ID&scope=identify&response_type=code&redirect_uri=$redirect`, 
  method: 'POST',
  body: new URLSearchParams(
    client_id: CLIENT_ID,
    client_secret: CLIENT_SECRET,
    grant_type: "authorization_code",
    redirect_uri: "https://ezapplications.live/api/discord/callback",
    code: req.query.code
  )
);
  const json = await response.json();
  res.redirect(`/?token=$json.access_token`);
));

router.get('/login', (req, res) => 
  res.redirect(`https://discordapp.com/api/oauth2/authorize?client_id=$CLIENT_ID&scope=identify&response_type=code&redirect_uri=$redirect`);
);

module.exports = router;

这是我的 Server.js 代码

const express = require('express');
const path = require('path');

const app = express();

app.get('/', (req, res) => 
  res.status(200).sendFile(path.join(__dirname, 'index.html'));
);
app.use('/api/discord', require('./api/discord'));

app.use((err, req, res, next) => 
  switch (err.message) 
    case 'NoCodeProvided':
      return res.status(400).send(
        status: 'ERROR',
        error: err.message,
      );
    default:
      return res.status(500).send(
        status: 'ERROR',
        error: err.message,
      );
  
);

app.listen(50451, () => 
  console.info('Running on port 50451');
);

这里是 utils.js 的代码

// async/await error catcher
const catchAsyncErrors = fn => (
  (req, res, next) => 
    const routePromise = fn(req, res, next);
    if (routePromise.catch) 
      routePromise.catch(err => next(err));
    
  
);

exports.catchAsync = catchAsyncErrors;

我不知道为什么会出现这个错误,因为我觉得一切都很好,谢谢。

【问题讨论】:

【参考方案1】:

根据Authorization Code Grant 文档,您需要在请求正文中传递参数而不是查询字符串。

const response = await fetch("https://discord.com/api/oauth2/token", 
  method: 'POST',
  headers: 
    "content-type": "application/x-www-form-urlencoded"
  ,
  body: new URLSearchParams(
    client_id: CLIENT_ID,
    client_secret: CLIENT_SECRET,
    grant_type: "authorization_code",
    redirect_uri: "https://ezapplications.live/api/discord/callback",
    code: req.query.code
  ).toString()
);

关于 HTTP 基本认证...

您还可以通过client_idclient_secret 作为基本身份验证,使用client_id 作为用户名和client_secret 作为密码

...但我真的不明白这一点。你的选择

【讨论】:

现在我得到错误 "status":"ERROR","error":"body 选项必须是流。可读、字符串或缓冲区" 我更新了问题中的原始代码,让你看看我放了什么 您使用的是fetch 的哪个实现?我已经用fetch 参数更新了我的答案更明确(注意headerstoString() @Bobjoe12 如果您使用的是node-fetch,那么您对我的原始代码应该没有任何问题。见npmjs.com/package/… 我正在使用 got。当我使用 nodefetch 时,我收到一个关于 es 的错误,以及 require 如何不能使用它。我用 .toString 和标题更新了我的代码,但现在我收到错误 "status":"ERROR","error":"Response code 401 (Unauthorized)"

以上是关于不和谐的oauth中的错误请求的主要内容,如果未能解决你的问题,请参考以下文章

错误 redirect_uri:“不是格式正确的 URL。”在不和谐的 OAuth2 中

Cors 阻止不和谐 oauth2 登录

有没有办法使用不和谐 API 中的机器人令牌获取机器人数据?

如何获取不和谐 oauth2 隐式授权的范围值?

GitHub页面的不和谐Oauth2?

你如何为你的不和谐机器人创建一个永久的 oauth 邀请?