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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Sreenatha
New Member

PowerBINotAuthorizedException exception when executing admin/activityevents API

I am trying to collect powerbi usage via the admin/activityevents API but I get PowerBINotAuthorizedException in the response.

Steps followed to generate the token:

Created an azure AD app and assigned Tenant.Read.All and Tenant.ReadWrite.All permissions to it and granted the permissions.

Generated the token using oauth2/v2.0/token api where I used grant_type as client_credentials and scope as https://analysis.windows.net/powerbi/api/.default. I got PowerBINotAuthorizedException exception.

 

Further I tried to use Service Principal to hit the API using below steps:

1) Added new security group and added the App as member of it.

2) In PowerBi Tenant settings, allowed ServicePrincipal to hit PowerBI and also ReadOnly admin APIs.

 

Generated the token and hit the activityevent api but still got same PowerBINotAuthorizedException.

 

Though I was able to hit the API with grant_type password, but we do not want to use that approach.

What am I missing? Is it possible to use app authentication/service principal authentication to hit admin APIs specifically activityevents api?

My main usecase is to get the last activity date of every powerbi user.

5 REPLIES 5
Anonymous
Not applicable

Here is the content I have posted after finally resolving authentication issues in rest api and making the program work.

 

https://community.powerbi.com/t5/Service/Automate-importing-PowerBI-audit-logs-using-Application-or/...

 

Anonymous
Not applicable

Hi Guys

I wanted to share information about the automation of importing PowerBI audit logs using Application Permissions or Service Principle from PowerBI service right from App registration to using MSAL python library. There are several documentations available in Microsoft site with different configurations, but unless we search them with specific term and follow the instructions I was not able to resolve the authentication issues.

 

Since most of the company's we use multifactor authentication, delegated permissions will not allow any python/.net application to get automated. 

 

 

The first and foremost thing we need to perform is registering an app in Azure portal or using app registration using following link https://dev.powerbi.com/apps

 

App Registration:

Perform app registration steps only as mentioned in the following blog (https://carldesouza.com/how-to-register-an-azure-app-to-use-with-power-bi/) and DO NOT set any permissions. Since these permissions didn't worked while authenticating the token generated.

 

Permissions:

  1. After assigning Tenant.Read.All make sure you are Not accepting the admin consent as suggested by below documentation.

https://docs.microsoft.com/en-us/power-bi/admin/read-only-apis-service-principal-authentication

  1. Create Client Secret and keep this handy.

 

Making API calls using MSAL library:

  1. MSAL client secret example can be found here

https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/dev/sample/confidential_...

 

  1. Create json parameter file(Parameters.json) with following configurations and endpoints

{    "authority": "https://login.microsoftonline.com/Enter you tenantID Here ",

    "client_id": "Enter ClientID of the app you have Registered",

    "scope": ["https://analysis.windows.net/powerbi/api/.default"],

    "secret": "Enter Client Secret you created above",

    "endpoint": "https://api.powerbi.com/v1.0/myorg/admin/groups?$expand=datasets,dataflows,reports,dashboards,users&..."

 

 

}

  1. Install necessary MSAL library for Python using "pip install msal" in your command prompt
  2. You can get the code I have used to generate user activity  data from below git link. Even though I have mentioned an endpoint in parameters file I am deriving my required user activity endpoint in Python program since I have to generate several extracts with several endpoints and I want to use a single parameter files for all those extracts.

 

Note: There might be better ways to program in Python to extract the data from these API's but this is what I have developed which is working successfully.

 

Anonymous
Not applicable

I am also getting the following response

{
"error": {
"code": "PowerBINotAuthorizedException",
"pbi.error": {
"code": "PowerBINotAuthorizedException",
"parameters": {},
"details": [],
"exceptionCulprit": 1
}
}
}
 
Got the auth token by giving resource: https://analysis.windows.net/powerbi/api. Did anyone find the solution for it for the get activity API
Anonymous
Not applicable

@Sreenatha 

Did you ever get this PowerBINotAuthorizedException error resolved.

I am currently trying to extract audit logs using service principle for automation and performed all configurations same like you and running into not authorized issue.

 

PowerBINotAuthorizedException

When I use the token generated by my userid and password, the generated token was working and extracting logs. running into issues when token generated by service principle is throwing not authorized error.

 

v-lionel-msft
Community Support
Community Support

Hi @Sreenatha ,

 

Maybe you can refer to the link.

Power BI Connector PowerBINotAuthorizedException 

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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