Management API returns 400/Connection refused

I’m trying to use the management API to trigger an automatic update of all users upon first login after signup. I’m trying to do that from my own API (rather than via hooks or rules) because there are a number of things I need to do after signup, so I’ve been trying to use the Management API.

My app uses Flask for its backend and Angular 9 for the frontend. When I try to send the request from my API (using the template from this guide), Flask returns ConnectionRefusedError: [Errno 61] Connection refused. I thought I might’ve made a mistake so I tried curl (with the format copied from the same guide), but when I tried it, I got, {"statusCode":400,"error":"Bad Request","message":"Invalid request payload JSON format"}.

I’m not sure what I’m doing wrong, especially since I’ve literally copied the template from the guide and it still seems to be in the wrong format. Can anybody help, please?

Thank you so much in advance!

1 Like

Check the tenant logs for errors.

If nothing is available there, copy and paste your curl command here.

John

Thanks for the response!

So curl seems to be going through okay now, but requests from my Python backend still return ‘connection refused.’ Since the connection is refused, there’s nothing in the logs, either. I’m not entirely sure what the issue is.

This is my script:

connection = http.client.HTTPSConnection("")
create_payload = "{ \"roles\": [ \"rol_BhidDxUqlXDx8qIr\" ] }"
auth_header = "Bearer " + os.environ.get('MGMT_API_TOKEN')
headers = {
    'content-type': "application/json",
    'authorization': auth_header,
    'cache-control': "no-cache"
}
connection.request("POST", "https://" + AUTH0_DOMAIN + "/api/v2/users/" + user_data['id'] + "/roles", create_payload, headers)
create_response = connection.getresponse()
create_response_data = create_response.read()
print(create_response_data)

As said before, it’s basically copied from the guide, so I’m not sure sharing it would make a difference, but if there’s any error in it…

1 Like

Having the exact same issue (straight from the guide https://auth0.com/docs/tokens/management-api-access-tokens/get-management-api-access-tokens-for-production)

Hey there @john.gateley can you followup on that? Thanks!

Hi y’all,

I would try:

  • Verify that curl works
  • Verify that your application doesn’t work
  • Print out the URL used by your application, and token if needed
  • Try the URL/token from your application via curl ON THE APPLICATION SERVER.
    This should help you track down the error.

John

1 Like

Hi,

So I’ve tried curl again (still works), and I’ve tried the application again. The application still doesn’t work - I have no idea why. Even when I just try to get Management API access token, I still get a ‘connection refused’ error.

Interestingly, when I’ve tried using the urllib.request module instead of the http.client module, like you suggested, it did work. I’ve just tested it a second time and it still does work. But I’m still not sure why it doesn’t work the way it’s written in the Auth0 guides. I’ve even tried to just copy the code snippets as they are to separate Python scripts - no app or anything - and it still doesn’t work, which makes me think something may have changed in the HTTP client module. Or maybe there’s something missing?

Thanks!

Hi @shirb,

I would try a network sniffer: trace the traffic with cURL, then trace with your application and see what is different. It sounds like your app is not connecting to the right server or is connecting on the wrong port.

John

Hi @john.gateley,

It took a bit more testing (of other URLs) and digging into the Python documentation, but I finally figured out the issue: it’s your (Auth0’s) guide. It seems to be outdated. The correct code snippet would be (note the location of the Auth0 domain):

connection = http.client.HTTPSConnection(AUTH0_DOMAIN) ## this is the different part
create_payload = "{ \"roles\": [ \"rol_BhidDxUqlXDx8qIr\" ] }"
auth_header = "Bearer " + MGMT_API_TOKEN
headers = {
    'content-type': "application/json",
    'authorization': auth_header,
    'cache-control': "no-cache"
}
## and this is also different
connection.request("POST", "/api/v2/users/" + user_data['id'] + "/roles", create_payload, headers) 
create_response = connection.getresponse()
create_response_data = create_response.read()
print(create_response_data)

I’d recommend fixing the Python snippet in the guide, though, to prevent further issues.

1 Like

Glad you have figured it out and thanks for sharing with the rest of community! I’ll relay that info to our docs team.

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