错误403尝试通过Active Directory访问Azure Databricks API时未授权用户

问题描述

我一直在关注文档:https://docs.microsoft.com/en-us/azure/databricks/dev-tools/api/latest/aad/service-prin-aad-token

创建服务主体并使用它来访问Databricks。我已经配置了databricks工作区,并使用它来创建集群。然后,我按照文档中提到的过程创建了服务主体,并获得了两个令牌:AD Access令牌和管理访问令牌。但是,我无法使用该API。

配置后的最终调用cURL命令:

curl -X GET \
-H 'Authorization: Bearer <access-token>' \
-H 'X-Databricks-Azure-SP-Management-Token: <management-access-token>' \
-H 'X-Databricks-Azure-Workspace-Resource-Id: /subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Databricks/workspaces/<workspace-name>' \
https://<databricks-instance>/api/2.0/clusters/list

使用令牌和其他信息替代后,将提供以下结果:

<html>
<head>
<Meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 403 User not authorized.</title>
</head>
<body><h2>HTTP ERROR 403</h2>
<p>Problem accessing /api/2.0/clusters/list. Reason:
<pre>    User not authorized.</pre></p>
</body>
</html>

我已经尝试了一些方法解决此问题,即使用authorization_code授予类型将原始访问令牌请求更改为使用交互式流,依此类推,但这仅给了我Invalid access token错误

上述配置是否有问题?我缺少一些权限吗?

更新:来到访问控制(IAM)页面,该应用似乎未包含在其中。

解决方法

您必须为服务主体授予角色。

这是我未向服务主体授予角色的测试结果。该错误与您的错误相同:

enter image description here

接下来,按照以下过程将角色授予服务主体:

Azure门户 Azure Databricks Azure Databricks服务> 访问控制(IAM)>添加角色分配> 选择要授予的角色并找到服务负责人> 保存

enter image description here

最后,使用服务主体获取令牌。(别忘了授予服务主体权限并征得管理员同意)

enter image description here enter image description here

获取Azure Active Directory访问令牌: enter image description here 获取Azure管理资源终结点令牌: enter image description here 使用管理端点访问令牌来访问Databricks REST API: enter image description here