Error at requesting Management API Access Token for Production using Node.js

I tried to use the automatic code for Node.js to Get Management API Access Tokens for Production as shown in https://auth0.com/docs/tokens/management-api-access-tokens/get-management-api-access-tokens-for-production.

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

    var options = {
      method: 'POST',
      url: 'https://my-apps.us.auth0.com/oauth/token',
      headers: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        grant_type: 'client_credentials',
        client_id: 'my-client-id',
        client_secret: 'my-client-secret',
        audience: 'https://my-apps.us.auth0.com/api/v2/'
      }
    };

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

When I run this code, I get “Error: Request failed with status code 401”. and it points to the line console.error(error); it says: undefined.

Then I decided to test this request using python to check if I was passing the wrong data and in python I was able to retrieve the access token. The python code:

######################
import requests

payload = {
    'grant_type': 'client_credentials',
    'client_id': 'my-client-id',
    'client_secret': 'my-client-secret',
    'audience': 'https://my-apps.us.auth0.com/api/v2/'
}

response = requests.post(url='https://my-apps.us.auth0.com/oauth/token',
                         headers={'content-type': 'application/x-www-form-urlencoded'},
                         data=payload).json()

print(response)
##################

I’m not used to javascript, so is there a known problem with axios or Node.js? Should I import or install some module or dependency? I need to use this in javascript


This seems to be the same issue as mentioned at (When oauth/token is called, you can't access) where axios requires specific handling for sending data with the content type being stated in the header.

The above would mean that the sample code is likely broken; can you attempt to perform the suggestion provided in that linked question. I’ll also try to test this myself so I can request a docs update.

1 Like

Thank you! It looks like I had the same issue with the header.

But I went for a slightly different solution.

axios.post(https://my-apps.us.auth0.com/oauth/token,{
grant_type: ‘client_credentials’,
client_id: “my_client_id”,
client_secret: “my_client_secret”,
audience: “https://my-apps.us.auth0.com/api/v2/”
}, {
headers: {‘content-type’: ‘application/json’}
}).then(res => console.log(res)).catch(err => console.log(err));

1 Like

Glad you have it working and thanks for sharing with the rest of community!

Yes, using JSON as the content type and sending JSON data is also doable; the OAuth 2.0 token endpoint specifies the use application/x-www-form-urlencoded, but to my knowledge there’s no plans on our side to stop accepting JSON. I’ll put a note in my backlog to check the sample and request a documentation change.

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