azure - 通过 az ad 应用权限添加 Graph API 失败

标签 azure azure-cli azure-cli2

我正在尝试通过 CLI 2.x 添加图形 API。这是我正在运行的 PowerShell 脚本:

    #
    # (1) Register the app, replyUrl, enable implicitflow
    #
    Write-Host " -  Create Application " + $appName
    az ad app create --display-name "$appName" --reply-urls "$replyUrl" --oauth2-allow-implicit-flow true

    #
    # (2) get the app id into a variable
    #
    $appId=$(az ad app list --display-name $appName --query [].appId -o tsv)

    #
    # (3) API Permissions, add Graph API/Permission (delegated)
    
    #
    Write-Host " -  Add Graph API/Permission (delegated)"
    az ad app permission add --id $appid --api 00000002-0000-0000-c000-000000000000 --api-permissions 311a71cc-e848-46a1-bdf8-97ff7156d8e6=Scope
    
    #
    # (4) Grant permissions based on the error/warning from the previous step
    
    #
    Write-Host " -  Grant permissions"
    az ad app permission grant --id $appid --api 00000002-0000-0000-c000-000000000000

我从此 link 中提取了 --api-permissions id 。 脚本行 az ad apppermission add 抛出此错误(或警告):

az : Invoking az ad app permission grant --id xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx --api 00000002-0000-0000-c000-000000000000 is needed to make the change effective At C:\temp\CP\CreateAppRegistration.ps1:42 char:5 az ad app permission add --id $appid --api 00000002-0000-0000-c00 ... CategoryInfo : NotSpecified: (Invoking "az ad...hange effective:String) [], RemoteException FullyQualifiedErrorId : NativeCommandError

然后,我尝试调用错误 az ad app 权限授予 中的脚本,并收到以下错误:

az : Operation failed with status: 'Not Found'. Details: 404 Client Error: Not Found for url: https://graph.windows.net/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/oauth2PermissionGrants?$filter=clientId%20eq%20%27e62c4745-cccc-cccc-cccc-71e5599261fc%27&api-version=1.6 At C:\temp\CP\CreateAppRegistration.ps1:45 char:5 az ad app permission grant --id $appid --api 00000002-0000-0000-c ... CategoryInfo : NotSpecified: (Operation faile...api-version=1.6:String) [], RemoteException FullyQualifiedErrorId : NativeCommandError

有人可以帮助我了解我是否需要根据上面#3 生成的错误执行脚本(上面#4)吗?
或者为什么上面的 #3 返回错误/警告?

我说警告是因为 Graph API 似乎确实已添加,但我不确定它是否处于错误消息的正确状态。

az ad app permission grant --id xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx --api 00000002-0000-0000-c000-000000000000 is needed to make the change effective

最佳答案

理论上,您需要根据上面#3 生成的警告执行脚本(上面#4)。

您收到“404 客户端错误:未找到 url”表示端点 https://graph.windows.net/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/oauth2PermissionGrants?$filter=clientId%20eq%20%27e62c4745-cccc-cccc- cccc-71e5599261fc%27&api-version=1.6 返回空结果。

cmd az ad app 权限授予 将首先查询它,然后插入新权限。错误发生在查询步骤。我认为这不合理。您的要求是添加权限授予,但此cmd需要首先查询现有的权限授予。如果结果为空,您将无法添加它。

因此,此 cmd az ad app 权限授予 的逻辑目前并不完美。它可能更适合现有的 Azure AD 应用程序(具有服务主体),但不适用于新创建的 Azure AD 应用程序(没有服务主体)。

解决方法是使用 az ad apppermission admin-consent --id $appid 而不是 az ad apppermission grant。请参阅引用here 。它涵盖了 az ad app 权限授予 可以执行的操作。

执行一次 az ad apppermission admin-consent 后,它将为 Azure AD 应用生成服务主体,然后您可以使用 az ad apppermission grant > 稍后。

关于azure - 通过 az ad 应用权限添加 Graph API 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62883453/

相关文章:

azure - 如何在 Azure API 管理中向产品添加策略

azure - AZ CLI : how to add tags when creating SQL server

azure - 如何获取Azure数据工厂数据集、触发器、管道和链接服务的计数?

azure - 如何安装 Azure CLI 计费帐户

Azure CLI 从表中选择一个选项

azure - 如何使用 azure CLI 检查 azure 资源是否已在使用?

c# - 项目中不存在目标 "RunResolvePublishAssemblies"

c# - 维护网站的在线 (Azure) 和离线版本

azure - 适用于 5xx(503 除外)的 Terraform Azure 警报

azure - AZ REST DevOps 方法返回 "az : Not a json response"