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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

Error accessing Power BI REST API: Admin - Get Activity Events using Service Principal auth

Hello,

 

I try to access the Get Activity Events using Service Principal auth with Python.

I did succesfully manage to obtain the token, however using below's code I get the following error upon retrieving activity events from the API..

 

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

 

import adal
import requests
import json
import datetime

tenant_id = ""
authority_url = 'https://login.microsoftonline.com/'+tenant_id+'/'
resource_url = 'https://analysis.windows.net/powerbi/api'
client_secret = ""
client_id = ""
context = adal.AuthenticationContext(authority=authority_url,
                                     validate_authority=True,
                                     api_version=None)
token = context.acquire_token_with_client_credentials(resource_url, client_id, client_secret)
access_token = token.get('accessToken')

print(requests.get(f"https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime='2021-07-22T00:00:00Z'&endDateTime='2021-07-22T23:59:59Z'", headers = {'Authorization': f'Bearer {access_token}'}).json())

 

- I enabled Service Principal Read Only API in the admin portal in Power BI.

- I added the App Registration to the security group that is assigned in the setting

 

What am I doing wrong?

1 ACCEPTED SOLUTION
chrisarys
New Member

we had the same issue and after investigations with MS support we found out that the Admin Consent should be removed from the app permissions.  Once removed the 

PowerBINotAuthorizedException

error was gone !

 

Hope it helps ?

View solution in original post

4 REPLIES 4
DavidCousinsT
Advocate I
Advocate I

If chrisarys response doesnt help, try changing resource_url to https://analysis.windows.net/powerbi/api/.default

 

I had to include the .default bit at the end of the URL to ensure the token generated gave access to all of the Power BI api.

chrisarys
New Member

we had the same issue and after investigations with MS support we found out that the Admin Consent should be removed from the app permissions.  Once removed the 

PowerBINotAuthorizedException

error was gone !

 

Hope it helps ?

I'm facing the same issue, can you give me more information on how to do this please?

v-shex-msft
Community Support
Community Support

Hi @Anonymous,

Have you worked with the account that has enough permission to operation and traces these activity logs? Did these contents managed in premiums capacity? 

Please share more detailed information to help us clarify this scenario:

How to Get Your Question Answered Quickly  

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.