繁体   English   中英

如何使用 Python 从 Auth0 获取用户列表和总数

[英]How to get List of users and total from Auth0 using Python

如何从 Auth0 帐户获取所有用户的列表,并使用 PYTHON 语言获取总数?

Auth0 允许使用 API 令牌来获取数据。 要获取包括总数在内的用户详细信息,您将需要 API 客户端密钥和 ID。

Auth0 文档获取令牌

我在这里写了一篇文章

要使用的代码...

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

在启用测试应用程序并在此处授予读取权限后让sydadder代码工作:Auth0 仪表板/应用程序/API/机器到机器应用程序/Auth0 管理 API(测试应用程序)(通过单击向下箭头展开)

在不调整权限的情况下,我得到了:

{'statusCode': 401, 'error': 'Unauthorized', 'message': 'Invalid token', 'attributes': {'error': 'Invalid token'}}

再加上将“eu.auth0.com”更改为适当的 Auth0 区域。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM