API token using Javascript does not work but does in python

I am having a strange issue when it comes to getting an authorization token. In my code I use axios typically but have done also tried it using request as given in the generated example when viewing it under Applications->APIs

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

var options = {
  method: 'POST',
  url: 'https://dev-8ciw43-h.us.auth0.com/oauth/token',
  headers: {'content-type': 'application/json'},
  data: {
    grant_type: 'client_credentials',
    client_id: 'xxxxx',
    client_secret: 'xxxxx',
    audience: 'https://dev-8ciw43-h.us.auth0.com/api/v2/'
  }
};

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

I get a 401 error when running this
However if I use the same tool it works in python with no issues

I am trying to fix an app where someone hardcoded the token as a parameter.

Hi @nutri.kinetix.adm

Could you try the following and let me know if it works?

var options = {
  method: 'POST',
  url: 'https://{AUTH0_DOMAIN}/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://{AUTH0_DOMAIN}/api/v2/'
  })
};

Kind Regards,
Nik

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