从 API 调用获取 AzureAD/EntraID 应用程序属性

问题描述 投票:0回答:1

我对 AzureAD 完全陌生,想使用 API 调用来测试我们正在使用的 EntraID 应用程序的配置是否正确。 我们的身份团队创建了一个附加到我想要查询的 entraID 应用程序的服务主体。我使用此服务主体 clientId、clientSecret 和我们的tenantId 来生成图形客户端(请参阅下面的 python 代码快照)

        client_id = self.settings['clientId']
        tenant_id = self.settings['tenantId']
        client_secret = self.settings['clientSecret']

        client_credential = ClientSecretCredential(tenant_id, client_id, client_secret)
        graph_client = GraphServiceClient(client_credential) 

然后,为了获取我的 entraID 应用程序属性,我按照此处记录的步骤进行操作:https://learn.microsoft.com/en-us/graph/api/application-get?view=graph-rest-1.0&tabs=python#请求

result = await graph_client.applications.by_application_id('my-entraID-applicationID').get()

但它不起作用,我收到一个

RessourceNotFound
错误:

Request_ResourceNotFound Resource 'xxxxxxxxxxxxxxxxx' does not exist or one of its queried reference-property objects are not present.

你知道我做错了什么吗? (该应用已被授予API权限Application.Read.All、Group.Read.All、GroupMember.Read.All)

azure-active-directory azure-ad-graph-api azure-entra-id
1个回答
0
投票

我同意@juunas,错误发生是因为您使用Entra ID应用程序注册的Application ID,而不是Object ID。

我有一份 Entra ID 申请,其中授予了

Application
类型的 Application.Read.All 权限:

enter image description here

最初,当我尝试通过传递应用程序 ID 来获取应用程序属性时,我也遇到了同样的错误

result = await graph_client.applications.by_application_id('my-entraID-applicationID').get()

回复:

enter image description here

要解决该错误,您需要传递Entra ID应用程序注册的对象ID,可以在此处找到:

enter image description here

当我通过将应用程序 ID 替换为 对象 ID 来运行下面的 python 代码时,我成功获得了具有应用程序属性的 响应,如下所示:

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

tenant_id = "tenantId"
client_id = "clientId"
client_secret = "clientSecret"

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

graph_client = GraphServiceClient(client_credential)

async def main():
    try:
        result = await graph_client.applications.by_application_id('my-entraID-objectID').get()
        print(result)
    except Exception as e:
        print(e)

asyncio.run(main())

回复:

enter image description here

参考: 获取应用程序 - Microsoft Graph v1.0

© www.soinside.com 2019 - 2024. All rights reserved.