Problems with getting userinfo from management API

Hi,

I’m having some problems synchronizing my auth0 userinfo with my local database. I have implemented this code;

userRouter.get("/sync", validateAccessToken, (req, res) => {
  var request = require("request");
  var usertoken;
  var options = { method: 'POST',
    url: 'https://MYDOMAN.eu.auth0.com/oauth/token',
    headers: { 'content-type': 'application/json' },
    body: '{"client_id":"myclienttoken","client_secret":"myclientsecret","audience":"https://MYDOMAIN.eu.auth0.com/api/v2/","grant_type":"client_credentials"}' };
  
  request(options, function (error, response, body) {
    if (error) throw new Error(error);
    usertoken = body;
    console.log(body);
  });
  var auth0options = {
    method: "GET",
    url: "https://MYDOMAIN.eu.auth0.com/api/v2/users",
    params: {id: 'email:"testuser"', search_engine: 'v3'},
    headers: {
      "content-type": "application/json",
      authorization: `Bearer` + usertoken.access_token,
    },
  };

  axios.request(auth0options).then(function (response) {
      console.log("RES DATA: ", response.data);
    })
    .catch(function (error) {
      console.error(error);
    });
  console.log("called");
  res.status(200).json("message");
});

But it dosen’t seem to work. I get the access token from this line:

usertoken = body;

But I don’t get the userinfo when calling the auth0 api with that token. I’m using the audience from the Auth0 Management API ex:

https://MYDOMAIN.eu.auth0.com/api/v2/

And not the audience from my own API, as I have read that’s the correct way:

https://mydomain

Any ideas on what I’m doing wrong?

Hello @mathiasm welcome to the community!

Are you getting any error or error code that might be helpful in hinting at the issue? Does you M2M client in Auth0 have the right permissions(read:users and read:user_idp_tokens)? I’m not positive you can query by id - You may want to try email or user_id.

https://auth0.com/docs/manage-users/user-search/user-search-query-syntax#searchable-fields

Keep us posted!

Hi tyf.
I found out that the main problem was that I was not waiting for reply about the mgmt token in my code, before calling the v2/users endpoint. That resulted in an error. After I made sure first to call that endpoint only after I received the mgmt key, it worked :slight_smile:
Now I just need to figure out why I’m not receiving all user data in my result.

Thank you for the help and great service.

1 Like

That’s great to hear! Thanks for following up with the community :rocket:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.