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

I get the 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']

    conn = http.client.HTTPConnection("{env.get("AUTH0_DOMAIN")}")
    headers = { 'authorization': "Bearer {}".format(AUTH0_ACCESS_TOKEN)}

    conn.request("GET", f"/api/v2/users/{id}", 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.

Hi @somethingwitty ,

Welcome to the Auth0 Community!

Could you try the following scripts and see if you can get the user information?

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

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

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

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

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

Thanks!

Hi, I’m still getting the same error json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) with your script. user_json still is blank

I think I figured out what was wrong with getting user information in callback.

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

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

The quickstart guide in Auth0’s console for a Machine to Machine app said to use HTTPConnection so that might need to be fixed.

1 Like

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