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?
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.
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?
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.
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.