Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
ubersparky
New Member

PowerBI API Returns "PowerBINotAuthorizedException" despite permissions

Hi - I am trying to access the API to download usage statistics from PowerBI and integrate with other reports. When I run the below code

 

 

 

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)

 

 

 When I print login_result I get what seems to be a correct response.

 

 

{'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'}

 

 

However when I print data I get a <401> error, and the json_data reads

 

 

b'{"error":{"code":"PowerBINotAuthorizedException","pbi.error":{"code":"PowerBINotAuthorizedException","parameters":{},"details":[],"exceptionCulprit":1}}}'

 

 


I have checked in Azure for the permissions. I have the Dashboard.Read.All permission.

ubersparky_0-1667294993315.png

Can anyone please let me know what I could be doing wrong?

1 ACCEPTED SOLUTION
mariussve1
Super User
Super User

When you use the admin api you need to grant access to this in the tenant admin. Because you use service principal this need to be added in a security group, and this group needs to be granted admin api access in the tenant admin portal.

When this is done, I,ve experienced it can take up to 20-30 min before it works.

 

Br

Marius


Br
Marius
BI Fabrikken
www.bifabrikken.no

View solution in original post

1 REPLY 1
mariussve1
Super User
Super User

When you use the admin api you need to grant access to this in the tenant admin. Because you use service principal this need to be added in a security group, and this group needs to be granted admin api access in the tenant admin portal.

When this is done, I,ve experienced it can take up to 20-30 min before it works.

 

Br

Marius


Br
Marius
BI Fabrikken
www.bifabrikken.no

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors