1

尝试使用 Databricks API 以编程方式处理资源。我正在使用此 microsoft 文档向服务主体进行身份验证。

https://docs.microsoft.com/en-us/azure/databricks/dev-tools/api/latest/aad/service-prin-aad-token

但我收到以下错误

"Invoke-RestMethod : {"error":"invalid_resource","error_description":"AADSTS500011: 在租户中找不到名为https://management.core.azure.com的资源主体"

这是我的完整脚本。我错过了什么?

$ApiCommand = "clusters/get"

$DataBrick = "https://adb-3522222096750220.0.azuredatabricks.net"

$DataBricksResourceID = ""

$VaultName = ""
$KeyName = ""

$apiEndpointUri = "https://management.core.azure.com"  
$tenantId = ""  
$applicationId = ""  
$secret = Get-AzKeyVaultSecret -VaultName $VaultName -Name $KeyName -AsPlainText

$RequestAccessTokenUri = "https://login.microsoftonline.com/$tenantId/oauth2/token"
$body = "grant_type=client_credentials&client_id=$applicationId&client_secret=$secret&resource=2ff814a6-3304-4ab8-85cb-cd0e6f879c1d" 
$Managementbody = "grant_type=client_credentials&client_id=$applicationId&client_secret=$secret&resource=$apiEndpointUri"  
$contentType = 'application/x-www-form-urlencoded' 

$AccessToken = Invoke-RestMethod -Method Post -Uri $RequestAccessTokenUri -Body $body -ContentType $contentType  
Write-Output $AccessToken
$ManagementToken = Invoke-RestMethod -Method Post -Uri $RequestAccessTokenUri -Body $Managementbody -ContentType $contentType
Write-Output $ManagementToken

$apiuri = $DataBrick +"/api/2.0/$ApiCommand"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer " + $AccessToken.access_token)
$headers.Add("X-Databricks-Azure-SP-Management-Token", $ManagementToken.access_token)
$headers.Add("X-Databricks-Azure-Workspace-Resource-Id", $DataBricksResourceID)

Invoke-RestMethod -Uri $apiuri -Headers $headers
4

1 回答 1

1

管理端点 URI 中的尾随/字符非常重要 - 您需要在文档中指定它:https://management.core.windows.net/

您还可以将此 SP 添加到工作区本身,然后您只需要获得一个 AAD 令牌(请参阅文档)。

于 2021-11-03T15:52:02.543 回答