首页 > 解决方案 > OAuth2:提供的`redirect_uri`与代码授权的内容不匹配

问题描述

我是 OAuth2 的新手,想了解为什么我会在浏览器主窗口和调试器控制台中看到此内容:

TokenError: The `redirect_uri` provided did not match what is authorized by the code.

脚步

  1. 我的前端 react.js 应用程序有一个链接到:https://app.asana.com/-/oauth_authorize?response_type=code&client_id=1188550654936298&redirect_uri=https%3A%2F%2Fasanarepeater.ngrok.io%2Foauth%2Fcallback&state=160063113436700

  2. 我设置了一个反向代理localhost:6500https://asanarepeater.ngrok.io

  3. 我通过开发控制台将上述 url plus/oauth连接到 oauth 服务(Asana),并接收回这个 url:

https://app.asana.com/-/oauth_authorize?response_type=code&client_id=1188550654936298&redirect_uri=https%3A%2F%2Fasanarepeater.ngrok.io%2Foauth%2F

我添加了一个状态查询参数,并尝试通过单击按钮在前端应用程序中使用它。

  1. OAuth 服务要求我登录

  2. 登录后,我在浏览器窗口和我的 express api 的调试控制台中看到以下内容:

TokenError: The `redirect_uri` provided did not match what is authorized by the code.
    at Strategy.OAuth2Strategy.parseErrorResponse (/home/owner/cp/project/node_modules/passport-oauth2/lib/strategy.js:358:12)
    at Strategy.OAuth2Strategy._createOAuthError (/home/owner/cp/project/node_modules/passport-oauth2/lib/strategy.js:405:16)
    at /home/owner/cp/project/node_modules/passport-oauth2/lib/strategy.js:175:45
    at /home/owner/cp/project/node_modules/oauth/lib/oauth2.js:191:18
    at passBackControl (/home/owner/cp/project/node_modules/oauth/lib/oauth2.js:132:9)
    at IncomingMessage.<anonymous> (/home/owner/cp/project/node_modules/oauth/lib/oauth2.js:157:7)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (_stream_readable.js:1220:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

反向 HTTPS 代理还显示:

GET /oauth/                    500 Internal Server Error

上面犯了什么错误?

我应该注意我看到在错误页面的 url 中返回了身份验证代码:

https://asanarepeater.ngrok.io/oauth/?code=1%2F11192028978382%3Af818bceafeef50d7fREMOVED

如果 Express.js 端点是相关的,我将它们包括在下面。两条路线都以/oauth

router.get(
  '/callback',
  passportWithAsanaStrategy.authenticate('Asana'),
  (req: Request, res: Response) => {
    const jwt = jsonwebtoken.sign((req.user as { asana_id: string }).asana_id, JWT_SECRET!);

    res
      .cookie('asanaIdJwt', jwt)
      .status(200)
      .redirect(FRONTEND_URL!);
  },
);

/*
  passport middleware redirects to the scope grant page

  thus no route handler callback
*/
router.get('/', passportWithAsanaStrategy.authenticate('Asana'));

标签: node.jsoauth-2.0

解决方案


通常使用 OAuth2,您在设置 OAuth2 凭据时选择 1 个特定的重定向 uri。如果设置期间的 url 与您在查询参数中发送的内容不匹配redirect_uri,OAuth2 服务器应该出于安全原因拒绝该请求。

所以,我认为您可能需要进入您的 asana OAuth2 设置并查看为redirect_uri.


推荐阅读