python - 如何使用 Python 从 Auth0 获取用户列表和总数
问题描述
如何从 Auth0 帐户获取所有用户的列表,并使用 PYTHON 语言获取总数?
解决方案
Auth0 允许使用 API 令牌来获取数据。要获取包括总数在内的用户详细信息,您将需要 API 客户端密钥和 ID。
我在这里写了一篇文章
要使用的代码...
import http.client
import json
client_id = 'xxxxxxxxxxxxxxxxxxxx'
client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
business_name = 'xxxxxxxxx'
# GET THE ACCESS TOKEN using the client_id and client_secret
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 AND TOTAL
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
推荐阅读
- java - 一款带有文字图形的 RPG 游戏
- c# - 如何从 C# 调用 PostgreSQL 13 存储过程?
- javascript - 在两个数组之间迭代以查找匹配值的最有效方法是什么?
- c# - C# TypeKind 的字符串?是 TypeKind.Class
- reactjs - 如何使用 React Flow 增加边缘下降区域
- django - 如何在 Django 中将用户发送的数据关联到他自己的帐户
- reactjs - 如何在 Monday.com 中使用列值创建新的板项目?代码不工作
- jquery - 无法从 Parsley AJAX 输入值验证器获得响应
- node.js - “属性或方法“产品”未在实例上定义,但在渲染期间引用“Vue App 中的错误
- azure-container-instances - ACI容器组退出成功后消失