Loading...

How to get the list of users from Auth0 using Python

Last updated: Oct 02nd 2024

You will need your Auth0 Application API Client ID and Client Secret which you could obtain from your Auth0 tennant settings.  You could use the following basic code to get the token and then get the list of users.

import http.client
import json

client_id = 'xxxxxxxxxxxxxxxxxx'
client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
business_name = 'xxxxxxxxxxxx'
conn = http.client.HTTPSConnection(business_name+".eu.auth0.com")
payload = "{\"client_id\":\""+client_id+"\",\"client_secret\":\""+client_secret+"\",\"audience\":\"https://"+business_name+".eu.auth0.com/api/v2/\",\"grant_type\":\"client_credentials\"}"
headers = { 'content-type': "application/json" }
conn.request("POST", "/oauth/token", payload, headers)
res = conn.getresponse()
data = res.read()
tokendetails_json = data.decode('utf8').replace("'", '"')
# Load the JSON to a Python list & dump it back out as formatted JSON
tokendetails = json.loads(tokendetails_json)


#NOW GET THE USERS
conn = http.client.HTTPSConnection(business_name+".eu.auth0.com")
payload = "{\"client_id\":\""+client_id+"\",\"client_secret\":\""+client_secret+"\",\"audience\":\"https://"+business_name+".eu.auth0.com/api/v2/\",\"grant_type\":\"client_credentials\"}"
headers = { 'content-type': "application/json","Authorization": "Bearer "+tokendetails['access_token'] }
conn.request("GET", "/api/v2/users?include_totals=true", payload, headers)
res = conn.getresponse()
data = res.read()
result_data_json = data.decode('utf8').replace("'", '"')

# Load the JSON to a Python list & dump it back out as formatted JSON
result_data = json.loads(result_data_json)
print(result_data)                  # Json list of all users
print(result_data['total'])         # Just the count of total users

SYED ABDUL QUADER Published on 01 Nov 2021
Edit
Comments
Guest User