没有这样做的文件,我发现这是不可接受的.

from msal import ConfidentialClientApplication
from msgraph import GraphServiceClient

client_id = ''
client_secret = ''
tenant_id = ''
authority = f'https://login.microsoftonline.com/{tenant_id}'
scopes = ['https://graph.microsoft.com/.default']

app = ConfidentialClientApplication(
    client_id,
    authority=authority,
    client_credential=client_secret,
)

response = app.acquire_token_for_client(scopes)

graph_client = GraphServiceClient(
    credentials=response,
    scopes=scopes
)


await graph_client.users.get()
/usr/local/lib/python3.10/dist-packages/kiota_authentication_azure/azure_identity_access_token_provider.py in get_authorization_token(self, uri, additional_authentication_context)
    101                 )
    102             else:
--> 103                 result = self._credentials.get_token(*self._scopes, claims=decoded_claim)
    104 
    105             if inspect.isawaitable(result):
> AttributeError: 'dict' object has no attribute 'get_token'

Analyzing the stack, you can see that the object passed as credentials in the `msgraph` client is not what it expects; `acquire_token_for_client` returns a dictionary, but `GraphServiceClient` expects it to have a function called "get_token."

如何解决这个问题?

推荐答案

我注册了一个Entra ID申请并授予了Application类型的User.Read.All个许可,如下所示:

enter image description here

最初,当我运行您的代码以获取用户列表时,我也得到了same error:

from msal import ConfidentialClientApplication
from msgraph import GraphServiceClient

client_id = ''
client_secret = ''
tenant_id = ''
authority = f'https://login.microsoftonline.com/{tenant_id}'
scopes = ['https://graph.microsoft.com/.default']

app = ConfidentialClientApplication(
    client_id,
    authority=authority,
    client_credential=client_secret,
)

response = app.acquire_token_for_client(scopes)

graph_client = GraphServiceClient(
    credentials=response,
    scopes=scopes
)


result = await graph_client.users.get()
print(result)

Response:

enter image description here

要解决该错误,请使用使用client credentials流的低于modified的代码成功与MS Shape和列出的用户进行身份验证:

import asyncio
from azure.identity import ClientSecretCredential
from msgraph import GraphServiceClient

tenant_id = "tenantID"
client_id = "appID"
client_secret = "secret"

credential = ClientSecretCredential(
    tenant_id=tenant_id,
    client_id=client_id,
    client_secret=client_secret
)

client = GraphServiceClient(credential)

async def main():
    result = await client.users.get()
    users = result.value

    for user in users:
        print("User ID:", user.id)
        print("User Display Name:", user.display_name)
        print("-" * 50)  # Separating each user with a line

asyncio.run(main())

Response:

enter image description here

References:

List users - Microsoft Graph

GitHub - microsoftgraph/msgraph-sdk-python

Python相关问答推荐

使文本输入中的文本与标签中的文本相同

提取两行之间的标题的常规表达

如何才能知道Python中2列表中的巧合.顺序很重要,但当1个失败时,其余的不应该失败或是0巧合

使用SciPy进行曲线匹配未能给出正确的匹配

在Python中处理大量CSV文件中的数据

Python中的嵌套Ruby哈希

优化pytorch函数以消除for循环

将pandas Dataframe转换为3D numpy矩阵

所有列的滚动标准差,忽略NaN

用砂箱开发Web统计分析

如何在Python中使用Pandas将R s Tukey s HSD表转换为相关矩阵''

pandas:对多级列框架的列进行排序/重新排序

在输入行运行时停止代码

为什么在FastAPI中创建与数据库的连接时需要使用生成器?

如何在海上配对图中使某些标记周围的黑色边框

pandas fill和bfill基于另一列中的条件

统计numpy. ndarray中的项目列表出现次数的最快方法

有没有一种方法可以在朗肯代理中集成向量嵌入

与同步和异步客户端兼容的Python函数

Django更新视图未更新