Auth0 节点 JS 身份验证拒绝使用 npm 请求
Auth0 NodeJS Authentification Refused using npm request
我遇到了问题,我尝试连接到 Auth0 API 以在我的 WebApp 上启用强识别。
上下文:
前端:我正在使用 angularJS 前端,并在那里实现了 Lock Library to manage the Auth0 popup by following this webapp-specific tutorial.
后端:NodeJS & Express 服务器,为了验证用户的身份验证,我使用 npm 库 "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
请求正文.
使用 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'
}
}
我遇到了问题,我尝试连接到 Auth0 API 以在我的 WebApp 上启用强识别。
上下文:
前端:我正在使用 angularJS 前端,并在那里实现了 Lock Library to manage the Auth0 popup by following this webapp-specific tutorial.
后端:NodeJS & Express 服务器,为了验证用户的身份验证,我使用 npm 库 "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
请求正文.
使用 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'
}
}