通过 az ad app 权限添加图表 API 失败
Add Graph API via az ad app permission fails
我正在尝试通过 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 app permission 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 permission grant
中调用脚本并得到以下错误:
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 返回 error/warning?
我说警告是因为图形 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
returns 空结果。
cmd az ad app permission grant
会先查询,再插入新的权限。错误发生在查询步骤中。我不认为这是合理的。您的要求是添加权限授予,但是此cmd需要先查询现有的权限授予。如果结果为空,它会阻止您添加它。
所以这个命令az ad app permission grant
的逻辑目前还不完善。它可能更适合现有的 Azure AD 应用程序(具有服务主体),但不适用于新创建的 Azure AD 应用程序(没有服务主体)。
解决方法是使用 az ad app permission admin-consent --id $appid
而不是 az ad app permission grant
。请参阅参考资料 here。它涵盖了 az ad app permission grant
可以做什么。
执行一次az ad app permission admin-consent
后,会为Azure AD应用生成一个服务主体,以后可以使用az ad app permission grant
。
我正在尝试通过 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 app permission 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 permission grant
中调用脚本并得到以下错误:
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 返回 error/warning?
我说警告是因为图形 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
returns 空结果。
cmd az ad app permission grant
会先查询,再插入新的权限。错误发生在查询步骤中。我不认为这是合理的。您的要求是添加权限授予,但是此cmd需要先查询现有的权限授予。如果结果为空,它会阻止您添加它。
所以这个命令az ad app permission grant
的逻辑目前还不完善。它可能更适合现有的 Azure AD 应用程序(具有服务主体),但不适用于新创建的 Azure AD 应用程序(没有服务主体)。
解决方法是使用 az ad app permission admin-consent --id $appid
而不是 az ad app permission grant
。请参阅参考资料 here。它涵盖了 az ad app permission grant
可以做什么。
执行一次az ad app permission admin-consent
后,会为Azure AD应用生成一个服务主体,以后可以使用az ad app permission grant
。