尝试使用 azure python sdk 在活动目录中列出用户时出错

Getting error while trying to list users in active directory using azure python sdk

我正在使用下面的一段代码:

from azure.identity import AzureCliCredential
from azure.graphrbac import GraphRbacManagementClient 
credential = AzureCliCredential()
tenant_id = ""
graph_client = GraphRbacManagementClient(credential,tenant_id)
users = graph_client.users.list()
for i in users:
    print (i.user_principal_name)

当我使用上面的代码时,出现以下错误:

AttributeError: 'AzureCliCredential' object has no attribute 'signed_session'

如果我删除用户分页上下文的迭代。然后没有错误,我得到输出:

<azure.graphrbac.models.user_paged.UserPaged object at 0x0000025125C1B250>

请帮助从分页上下文中获取用户列表。

我尝试使用相同的代码但使用服务主体进行身份验证。我收到一个错误,权限不足,因为 GraphrbacManagementClient 使用 Azure AD graph legacy API 权限获取已弃用的用户,我无法为服务主体添加权限。

作为解决方案,我使用 msgraph-core 模块来修复使用 Microsoft Graph API.

的问题

您可以使用以下命令安装软件包:

pip install msgraph-core

安装后你可以使用下面的代码:

from azure.identity import ClientSecretCredential
from msgraph.core import GraphClient 
clientid= "clientID or AppID"
clientsecret = "secret for the service principal"
tenantid = "tenantid"
credentials=ClientSecretCredential(tenant_id=tenantid,client_id=clientid,client_secret=clientsecret) 
graph_client = GraphClient(credential=credentials)
users = graph_client.get('/users?$select=userPrincipalName,id')
print(users.json())

输出: