python-3.x - 将 Azure AD 中的主体 ID 解析为用户、服务

标签 python-3.x azure azure-active-directory azure-ad-graph-api azure-rbac

我正在尝试将主体 ID 列表解析为用户/服务名称等详细信息。我有以下代码 -

from azure.common.credentials import ServicePrincipalCredentials
from azure.graphrbac import GraphRbacManagementClient

TENANT = 'something.onmicrosoft.com'
TENANT_ID = '...'
CLIENT_ID = '...'
SECRET = '...'
List_of_Principal_IDs= ['...','...']
credentials = ServicePrincipalCredentials(
    client_id=CLIENT_ID,
    secret=SECRET,
    tenant=TENANT_ID,
    resource="https://graph.windows.net"
)
client = GraphRbacManagementClient(credentials, TENANT)

我尝试遵循 stackoverflow pages 之一的建议但我遇到了错误(见下文)。任何有关我如何将这些主要 ID 解析为人类可理解的格式的指导将不胜感激。

users = client.users.list(
         filter=f"principal_id eq '{List_of_Principal_IDs[0]}'"
     )
test = users.next()

错误-

azure.graphrbac.models.graph_error_py3.GraphErrorException: Property 'principal_id' does not exist as a declared property or extension property.

users = client.objects.get_objects_by_object_ids(List_of_Principal_IDs[0])
user = users.next()

错误-

msrest.exceptions.SerializationError: Unable to build a model: Unable to deserialize to object: type, AttributeError: 'str' object has no attribute 'get', DeserializationError: Unable to deserialize to object: type, AttributeError: 'str' object has no attribute 'get'

最佳答案

azure.graphrbac.models.graph_error_py3.GraphErrorException: Property 'principal_id' does not exist as a declared property or extension property.

关于此错误,properties of users中不存在principal_id 。如果我没理解错的话,principal_id 是指用户的Object ID。但Object_id不支持filter,需要使用 get方法而不是 list方法。

user = client.users.get(upn_or_object_id)
<小时/>

msrest.exceptions.SerializationError: Unable to build a model: Unable to deserialize to object: type, AttributeError: 'str' object has no attribute 'get', DeserializationError: Unable to deserialize to object: type, AttributeError: 'str' object has no attribute 'get'

get_objects_by_object_ids需要GetObjectsParameters的参数类,但不仅仅是一个列表。

objects = graphrbac_client.objects.get_objects_by_object_ids({
    'object_ids': [list of object ids],
    'types': [list of object types]
})

关于python-3.x - 将 Azure AD 中的主体 ID 解析为用户、服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64866364/

相关文章:

使用 VSTS 中的服务主体的 Azure Graph 403 Authorization_RequestDenied

asp.net-mvc - Azure AD 登录后自动注销

python-3.x - 有没有办法在没有 Azure 的情况下通过 Power Automate 运行 python 脚本?

python - 如何从上传文件中删除内容处置

python-3.x - 这个python源代码有什么问题

Azure:我可以将 Web 作业部署到辅助角色吗?

python input() 在调用 input() 之前采用旧的标准输入

visual-studio-2010 - Windows Azure 培训套件的困难

azure - 如何将 Azure Functions v3 迁移到 .net core 5.0

java - 如何从 Azure key 保管库获取 Azure 存储帐户访问 key