Python Auth0: Unable to get user information via Management API V2 by user ID

I get the Auth0 management access token by:

conn = http.client.HTTPSConnection("{env.get("AUTH0_DOMAIN")}")

payload = "{\"client_id\":\"id\",\"client_secret\":\"secret\",\"audience\":\"https://{env.get("AUTH0_DOMAIN")}/api/v2/\",\"grant_type\":\"client_credentials\"}"

headers = { 'content-type': "application/json" }

conn.request("POST", "/oauth/token", payload, headers)

res = conn.getresponse()
data = res.read()
token_json = data.decode('utf-8').replace("'", '"')
token = json.loads(token_json)
AUTH0_ACCESS_TOKEN = token["access_token"]

which is successful and I get the management API access token.

The problem arises when I try to get user information during callback:

@app.route("/callback", methods=["GET", "POST"])
def callback():
    token = oauth.auth0.authorize_access_token()
    session["user"] = token
    id = session['user']['userinfo']['sub']

    payload = ''
    conn = http.client.HTTPConnection("{env.get("AUTH0_DOMAIN")}")

    headers = { 'authorization': "Bearer {}".format(AUTH0_ACCESS_TOKEN)}

    conn.request("GET", f"/api/v2/users/{id}", payload, headers=headers)

    res = conn.getresponse()
    data = res.read()
    user_json = data.decode('utf-8').replace("'", '"')
    user_data = json.loads(user_json)

The error occurs with the last line. json.loads yields an error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

When I checked the user_json, I find that it is empty meaning I didn't get a response from the Management API.

What am I doing wrong? Is there a formatting mistake? Am I not using the right user_id? For ID, I tried both session['user']['userinfo']['sub'] and session['user']['userinfo']['sub'].split('|', 1)[1] to exclude the prefix in the ID but still getting the same error.



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation