Getting 401 when sending the access token request for the mangement api

i wanted to extract the metadata for my next js blog app when i looked through the docs it says u can get it via the auth0 management Api i followed the docs here Get Management API Access Tokens for Production when i send the post request to get the access token to be able to use the managment Api

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

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

i get this error in the console

Hi @adhamdoody7813,

Thanks for reaching out to the Auth0 Community!

After looking through your code snippet, it seems like the error is derived from the headers.

When testing this locally on my machine, I got it working with the following code snippet:

const options = {
  method: 'POST',
  url: 'https://{YOUR_DOMAIN}/oauth/token',
  headers: {'content-type': 'application/json'},
  data: {
    grant_type: 'client_credentials',
    client_id: 'CLIENT_ID',
    client_secret: 'YOUR_CLIENT_SECRET',
    audience: 'https://{YOUR_DOMAIN}/api/v2/'
  }
};

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

Could you please try the code above and let me know how it works for you?

Thanks,
Rueben

@rueben.tiow i have made the changes as u mentioned but now i get this

keep in mind that i got the
header {'content-type': 'application/x-www-form-urlencoded'} from the docs

Hi @adhamdoody7813,

Thank you for your response.

It seems like you are making a request from the frontend client instead of the backend server, which throws the “blocked by CORS policy” error.

Given that, I recommend that you make the request on the backend to get the Management API’s Access Token.

When testing on my side, I used Postman to get a Management API access token.

Please let me know how this goes.

Thanks,
Rueben

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