Auth0 NodeJS身份验证拒绝使用npm请求

问题描述:

我面临一个问题,我试图连接到Auth0 API以在我的WebApp上启用强识别。Auth0 NodeJS身份验证拒绝使用npm请求

对于背景:

  • 前端:我使用的是angularJS前,有我实现了Lock库按照此web应用特定的tutorial管理Auth0弹出。

  • 后端:NodeJS &快速服务器,为了验证用户的认证,我使用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' 
     } 
     } 

然后我打电话请求()取回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" 
    } 

在我thougt这是我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请求主体

使用request package你应该做到以下几点:

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' 
    } 
}