我构建了一个 python 应用程序来访问只读 Power BI Rest API。我正在自动收集租户事件。然而,尽管配置了我的 Azure 应用程序并使用服务主体生成访问 token ,但我从 API 请求收到的响应是未经授权的响应之一:
{"error": {"code": "PowerBINotAuthorizedException", "pbi.error": {"code":
"PowerBINotAuthorizedException", "parameters": {}, "details": [], "exceptionCulprit": 1}}}
我在网上发现了许多类似的问题,但是我觉得我已经完成了建议的所有操作,但仍然无法使其正常工作。我将不胜感激任何指导。
我采取的步骤是:
配置了Azure应用程序,添加了Power Bi服务的应用程序权限-Tenant.Read.All
使用我的应用程序的 client_ID 和 client_Secret 根据客户端凭据流程请求我的访问 token ,如以下链接中所述:
https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow
我使用以下脚本成功接收了 token :
import requests
azureTenantID = "xxxxxxxxxxxxxxxxx"
azureClientId = "xxxxxxxxxxxxxxxxx"
azureClientSecret = "xxxxxxxxxxxxxxxxxx"
url = f"https://login.microsoftonline.com/{azureTenantID}/oauth2/v2.0/token"
payload = {
"grant_type": "client_credentials",
"client_id": azureClientId,
"client_secret": azureClientSecret,
"scope": "https://analysis.windows.net/powerbi/api/.default"
}
# Header HAS to be x-www-form-urlencoded for MS to accept it.
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
# Return POST content as JSON.
r = requests.post(url, data=payload, headers=headers).json()
# Grab the access token.
response = r.get("access_token")
# Concatenate with Bearer string
access_token = "Bearer {r['access_token']}"
配置了我的 Power BI 租户设置以使服务主体能够使用 API。
Screenshot of Admin API Setting
Screenshot of Developer API Setting
请注意,我将服务主体添加为启用了这两个设置的安全组的成员执行对 API 的 Get 请求
当我从 Power BI REST API 文档的“试用”功能中获取访问 token 时,以下脚本会返回良好的响应,但当我按照上面的方式生成 token 时,则不会返回良好的响应。
import requests
# Initialise parameters.
url = "https://api.powerbi.com/v1.0/myorg/admin/groups?$top=1000&$expand=datasets,dataflows,reports,users,dashboards"
headers = {'Authorization': get_access_token2()}
# Get response.
response = requests.get(url, headers=headers)
response = response.json()
如有任何帮助,我们将不胜感激!
最佳答案
我刚刚经历了您所描述的这个场景,最后我们不得不联系 Microsoft 支持人员来解决它。
尽管极其违反直觉,如果您为服务主体身份验证创建的应用分配有任何 Power BI 权限,则生成的访问 token (当传递到 Power BI REST Admin API 时)将返返回告 PowerBINotAuthorizedException
的错误响应。
更具体,如果您传递给 Power BI API 的访问 token 具有 roles
键/值对,那么您将获得一个 PowerBINotAuthorizedException
。
就您而言,问题更容易,因为您已经列出了您授予的权限。您提到您配置了 Azure 应用程序,添加了 Power Bi Service-Tenant.Read.All 的应用程序权限
。 为了解决此问题,您需要删除该权限。
对于 future 的读者,您可以通过使用 JWT token 解码器(如 jstoolset.com 中的解码器)解码访问 token 来解决此问题。 。如果您的应用已分配给您请求的范围的权限(https://analysis.windows.net/powerbi/api/.default 是您在授权中请求的典型 Power BI 范围)并且您解码 JWT token ,那么您将看到一个 roles
键/值对。这个角色
的存在本质上就是问题所在。那里的值可能与 Power BI REST Admin API documentation 中的Required Scope
匹配并不重要。 。我们向我们描述,好像您的访问 token 中有一个 roles
值,然后当该 token 提供给 Power BI API 时,会尝试使用授予的角色,这最终会导致PowerBINotAuthorizedException
因为不允许服务主体使用特定角色。
如果您已删除某个应用的所有权限,但在 roles
键/值对的访问 token 中仍有值,那么我建议您从一个新的应用开始应用程序没有分配任何权限,只需将新应用程序添加到您最初创建的现有安全组即可。这就是我们如何意识到这确实是问题所在,然后能够从那里进行协调。
编辑:Microsoft 现已更新相关端点上的 API 文档以反射(reflect)此信息。例如,在Admin - Groups GetGroupUsersAsAdmin中所需范围现在为:
Tenant.Read.All or Tenant.ReadWrite.All
Relevant only when authenticating via a standard delegated admin access token. Must not be present when authentication via a service principal is used.
关于python - Power BI Rest API 请求未按预期授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71001110/