簡體   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