我正在尝试访问API,以便从PowerBI下载使用情况统计信息,并与其他报告集成。当我运行以下代码时
import requests
import json
base_url = "https://api.powerbi.com/v1.0/myorg/admin/"
url = "https://login.microsoftonline.com/my_tenant_id_goes_here/oauth2/token"
scope = "https://analysis.windows.net/powerbi/api/.default"
grant_type = "client_credentials"
client_id = "my_client_id"
client_secret = "my_client_secret"
resource = "https://analysis.windows.net/powerbi/api"
header = {
"scope": scope,
"grant_type": grant_type,
"client_id": client_id,
"client_secret": client_secret,
"resource": resource
}
r = requests.post(url, data = header)
login_result = r.json()
print(login_result)
token_type = login_result.get("token_type")
access_token = login_result.get("access_token")
authorization_key = token_type + " " + access_token
print('Authentication Key Generated')
headers = {'Content-Type':'application/json','Authorization': authorization_key}
print('Consuming Dashboards Rest End Point')
data = requests.get(base_url + 'dashboards', headers=headers)
print(data)
json_data = data.content
print(json_data)打印后得到以下结果(Login_result)
{'token_type': 'Bearer', 'expires_in': '3599', 'ext_expires_in': '3599', 'expires_on': '1667296164', 'not_before': '1667292264', 'resource': 'https://analysis.windows.net/powerbi/api', 'access_token': 'longlongalphanumericstring'}它似乎找到了正确的访问令牌。但是,当我打印数据时,我得到一个<401>错误,json_data读取
b'{"error":{"code":"PowerBINotAuthorizedException","pbi.error":{"code":"PowerBINotAuthorizedException","parameters":{},"details":[],"exceptionCulprit":1}}}'我已签入Azure以获得许可。我有Dashboard.Read.All的许可。
当我将"admin“从
base_url = "https://api.powerbi.com/v1.0/myorg/admin/“
我得到了
'{"Message":"API is not accessible for application"}'在我看来,这是一个权限错误,但我似乎无法导航Azure界面来修复它。
发布于 2022-11-11 11:38:56
我试图通过邮递员在我的环境中复制相同的内容,并得到了如下所示的错误:

若要解决错误,请尝试以下操作:
我创建了一个Azure AD应用程序并授予了权限:

我创建了Azure安全组,并将服务主体添加为成员,如下所示:

在Allow service principals to use read-only admin APIs PowerBi Admin Portal中,启用PowerBi添加上面创建的安全组:

它需要围绕15 mins来反映设置。现在,我通过客户机凭据流生成令牌,如下所示:
我使用了v2.0令牌端点:
https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
client_id:clientID
client_secret:ClientSecret
grant_type:client_credentials
scope:https://analysis.windows.net/powerbi/api/.default

我能够成功地访问PowerBi ,如下所示:
https://api.powerbi.com/v1.0/myorg/admin/groups/ID

如果问题仍然存在,请尝试使用Tenant.ReadWrite.All为应用程序权限的授权代码流。
https://stackoverflow.com/questions/74276444
复制相似问题