Access_denied when trying to access management api token

When I am trying to get token with request it is working fine but when I am using axios it gives me error

const data = JSON.stringify({
  client_id: process.env.AUTH0_CLIENT_ID,
  client_secret: process.env.AUTH0_CLIENT_SECRET,
  audience: process.env.AUTH0_AUDIENCE,
  grant_type: "client_credentials",
});
const options = {
  method: "POST",
  url: "https://"+process.env.AUTH0_BASE_URL+"/oauth/token",
  headers: { "content-type": "application/json" },
  body: data,
};

when I run this code it works:

request(options, (err, response, body) => {
  if (err) {
    console.log(err);
  } else {
    console.log(JSON.parse(body));
  }
});

But when I run this it won’t

axios
  .request(options)
  .then((res) => {
    resolve(res);
  })
  .catch((err) => {
    console.log(err);
  });

Error:

data: { error: 'access_denied', error_description: 'Unauthorized' }

Hi there @cryogon welcome to the community!

I’m unfortunately not positive why that particular code isn’t working, it looks good to me :confused: Does the following work?

var axios = require("axios").default;

var options = {
  method: 'POST',
  url: 'https://{yourDomain}/oauth/token',
  headers: {'content-type': 'application/x-www-form-urlencoded'},
  data: new URLSearchParams({
    grant_type: 'client_credentials',
    client_id: '{yourClientId}',
    client_secret: '{yourClientSecret}',
    audience: 'https://{yourDomain}/api/v2/'
  })
};

axios.request(options).then(function (response) {
  console.log(response.data);
}).catch(function (error) {
  console.error(error);
});

Keep us posted!

Nope. It still doesn’t work

Edit:
I could of keep using request but when I am hosting it on vercel as a serverless function it does not work and gives an unauthorized error. So I was thing maybe this can be fixed with Axios.

Another Edit:
I switched to node-auth0 library and it works like request perfectly fine on localhost but on vercel it gives error

How should I use management api on vercel?

1 Like

Hey @cryogon thanks for following up!

Can you double check your client_id and client_secret in Vercel?

1 Like

Thanks for the reply @tyf.
I have doublechecked the client_id and client_secret and It is the same as my local environment variables. I don’t know what could be causing this problem.

Edit:
I think it has to be problem with environment variable on vercel when I hard coded value and deployed it on vercel it worked

1 Like

Thanks for the follow up @cryogon! I tend to agree as that’s typically the case in this sort of scenario :thinking:

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