简体   繁体   中英

Nodejs Auth0 read users with api not working (err: 400, msg: Bad HTTP authentication header format)

I'm trying to receive my users that are stored in Auth0. So, I tried using this website Auth0 management API docs with my API token and API domain. This works fine!
Then I tried to do the same in node js, but when I do that it returns an error. The error message is:

 "statusCode":400,"error":"Bad Request","message":"Bad HTTP authentication header format","errorCode":"Bearer" 

This is the code that i fount in the documentation

 var request = require("request"); var options = { method: 'GET', url: 'https://<api_url>/api/v2/users', headers: { authorization: 'Bearer ACCESS_TOKEN' } }; request(options, function (error, response, body) { if (error) throw new Error(error); console.log(body); }); 


The only thing that is changed is that I deleted the query string and inserted my api_url and the same access token that I used on the Auth0 management API docs (which works). Am I missing something?

The code looks perfectly fine. I edited it with my hostname/access token as follows and it returned the users:

var request = require("request");

var token = 'eyJ0...'

var options = { method: 'GET',
    url: 'https://tenant_name.auth0.com/api/v2/users',
    headers: { authorization: 'Bearer ' + token }
};

request(options, function (error, response, body) {
    if (error) throw new Error(error);

    console.log(body);
});

The two things that might have gone wrong in your case are:

  1. You didn't properly replace the ACCESS_TOKEN in the code.
  2. The access token does not have the read:users permission. You can verify this by pasting the token in jwt.io and inspecting the payload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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