azure - 使用 Microsoft Graph API 和 REST 方法从 Azure 门户应用程序注册中检索应用程序所有者

标签 azure powershell azure-active-directory microsoft-graph-api invoke-restmethod

我正在尝试通过 PowerShell 脚本使用 Microsoft Graph API 和 REST 方法从 list 下的 Azure 门户应用程序注册中检索应用程序所有者。我使用服务主体帐户(客户端 key )、租户和客户端 ID(应用 ID)对 Graph API 进行身份验证。

以下是获取特定应用信息的代码:

Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/applications?$filter=appId eq 'appIdhere'" -Headers $headers -Method GET

我正在使用以下请求 header 和正文:

$requestBody = @{
"grant_type"    = "client_credentials"
"client_id"     = $clientId
"client_secret" = $clientSecret
"scope"         = "https://graph.microsoft.com/.default"
}

$headers = @{
Authorization = "Bearer $accessToken"
}

但是,上面的代码仅适用于图表。请参阅下面的文档。 (我使用了/applications 端点,它仍然适用于下面的引用) Microsoft Graph API - 列出服务主体的所有者

我想使用上述 URL 访问应用注册 Azure 门户下的应用程序 list ,但在使用 PowerShell ISE 中的 Invoke-RestMethod 时,该 URL 会从 Azure 门户检索所有应用程序,而不是仅从 URL 检索指定的应用程序.

我想请求有关如何检索此 URL 下的指定信息的帮助:“https://graph.microsoft.com/v1.0/applications?$filter=appId eq 'f059f748-6b42-46ec-8d5b- 23a0fee126ee'”。预先感谢那些愿意提供帮助的人。

最佳答案

我注册了一个 Azure AD 应用程序并添加了 API 权限,如下所示:

enter image description here

现在,我将以下用户添加为应用程序的应用程序所有者:

enter image description here

要使用 Microsoft Graph API 从 list 下的 Azure 门户应用程序注册中检索应用程序所有者,您可以使用以下查询:

GET https://graph.microsoft.com/v1.0/applications/<AppID>/owners

要通过使用 PowerShell 调用 REST API 获得相同的结果,您可以执行以下脚本:

$requestbody = @{
    client_id = "ClientID"
    client_secret = "ClientSecret"
    scope = "https://graph.microsoft.com/.default"
    grant_type = 'client_credentials'
}
$AccessToken = Invoke-RestMethod -Uri "https://login.microsoftonline.com/TenantID/oauth2/v2.0/token" -Method Post -Body $requestbody
$token = $AccessToken.access_token
$query = "https://graph.microsoft.com/v1.0/applications/AppID/owners"
(Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $query -Method Get).value | select id

当我运行脚本时,我在响应中获得了应用程序所有者的对象 ID,如下所示:

enter image description here

引用: List owners - Microsoft Graph v1.0 | Microsoft

关于azure - 使用 Microsoft Graph API 和 REST 方法从 Azure 门户应用程序注册中检索应用程序所有者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76578185/

相关文章:

azure - 一封电子邮件中包含多个 HTML 表格,包含标题和表格

azure - 使用 php 将 .pbix (power BI) 文件导入工作区(多部分表单数据发布)

image - 就地编辑图像

azure - 如果我的客户端 key 过期,我可以重新生成访问 token 和刷新 token 对吗?

powershell - 命令行禁用netbios?

powershell - PowerShell 管道正在执行时没有垃圾收集

Azure:如何使用 Terraform 在 Azure 中设置多重身份验证策略?

c# - 在 Azure 函数中调用带有参数的存储过程会出现错误

azure - 无法创建 Azure WebJob : telling me the "triggered" folder already exists, 但事实并非如此

sql-server - 如何在 mssql(node.js) 中使用 Like 运算符