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

Preparing for a certification exam? Ask exam experts all your questions on May 15th. Register now.

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
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

Find out what's new and trending in the Fabric community.