繁体   English   中英

Auth0 NodeJS Authentification使用npm请求拒绝

[英]Auth0 NodeJS Authentification Refused using npm request

我遇到了问题,我尝试连接到Auth0 API以在我的WebApp上启用强识别功能。

对于上下文:

  • 前端:我正在使用angularJS前端,在那里我通过遵循这个特定于webapp的教程实现了Lock Library来管理Auth0弹出窗口。

  • 后端:NodeJS和Express服务器,为了验证用户的身份验证,我使用npm lib“request”来调用Auth0 API。

如果我理解的话,单击auth0小部件会向指定的端点URL发送请求,并且后端会收到它:

    app.get('/auth0CallbackURL', function (req, res) {
      console.log(req.query.code);
      var auth0code     = req.query.code;
      var client_secret = PROCESS.ENV.SERCRETID;
      var domain        = PROCESS.ENV.DOMAIN;
      var client_id     = PROCESS.ENV.CLIENTID;
      var redirectUrl   = PROCESS.ENV.REDIRECTURL;

      var request = require('request'); // request-promise
      var requestParams = {
        url: 'https://mycompanydomain.auth0.com/oauth/token?client_id='+client_id+'&redirect_uri='+redirectUrl+'&client_secret='+client_secret+'&code='+auth0code+'&grant_type=authorization_code',
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }

然后我调用request()来获取access_token并验证身份验证。

    request(requestParams, function(err, data) {
      if (err) {
      console.log('Err:', err);
      } else {
      console.log('response body: ', data.body)
      }

但我得到的唯一结果是:

    {
      "error": "access_denied"
      "error_description": "Unauthorized"
    }

在开始时,我认为我的Auth0配置不允许我的身份验证,但似乎没有问题。

提前感谢您的回复。

根据您链接的页面,您需要传递以下信息:

client_id=YOUR_CLIENT_ID
&redirect_uri=https://YOUR_APP/callback
&client_secret=YOUR_CLIENT_SECRET
&code=AUTHORIZATION_CODE
&grant_type=authorization_code

请求正文中 ,内容类型为application/x-www-form-urlencoded

您正在正确设置内容类型,但随后在URL查询组件中传递数据, 而您需要将其传递给POST请求正文

使用请求包,您应该执行以下操作:

var requestParams = {
    url: 'https://mycompanydomain.auth0.com/oauth/token',
    method: 'POST',
    body: 'client_id=' + client_id + 
        '&redirect_uri=' + redirectUrl + 
        '&client_secret=' + client_secret + 
        '&code=' + auth0code + 
        '&grant_type=authorization_code',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM