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 September 15. Request your voucher.

Reply
VSmart
Frequent Visitor

Get 401 Error when try to Get Activity Events (Audit Logs)

I'm trying to get Power BI audit logs using service principal.

I follow every step in creating Azure principal Embed Power BI content in an embedded analytics application with service principal and an applicatio...

1.Create app

2.Create secret 

3. Add app into group 

4. Give all permitions to app 

Tenant.Read.All, Tenant.ReadWrite.All;Report.ReadWrite.All, Report.Read.All, Dataset.Read.All;Auditlog.Read.All; Workspace.Read.All;Workspace.ReadWrite.All; and many other permitions.

5. Enable "Allow service principal to use Power BI API's" and "Enable content in apps" in Power BI admin portal.

 

I can get acces token and run few API calls like : 

My Python script is follow:

 

 

 

 

TENANT_ID = '1eb27ef3-976f-4600-a791-*******'
CLIENT_ID = 'd5376b05-a655-463b-ae9f-*******'
CLIENT_SECRET = '**************************'
SCOPE_BASE = ['https://analysis.windows.net/powerbi/api/.default']
AUTHORITY_URL = 'https://login.microsoftonline.com/organizations'
try:
    authority = AUTHORITY_URL.replace('organizations', TENANT_ID)
    clientapp = msal.ConfidentialClientApplication(CLIENT_ID, client_credential=CLIENT_SECRET, authority=authority)
    response = clientapp.acquire_token_for_client(scopes=SCOPE_BASE)
    try:
        access_token = response['access_token']
        activityDate = date.today() - timedelta(days=1)
        activityDate = activityDate.strftime("%Y-%m-%d")

        url = "https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime='" + activityDate + "T00:00:00'&endDateTime='" + activityDate + "T23:59:59'"
        header = {'Content-Type':'application/json', 'Authorization':f'Bearer {access_token}'}
        api_call = requests.get(url=url, headers=header)
        print(api_call)
        print(api_call.json())

    except KeyError:
        raise Exception(response['error_description'])

except Exception as ex:
    raise Exception('Error retrieving Access token\n' + str(ex))

 

 

 

 

 

 

4 REPLIES 4
Anonymous
Not applicable

Hi @VSmart,

Perhaps you can take a look at the following link that mentions about the similar issues:

Solved: Using a Service Principal for the Activity Events ... - Microsoft Power BI Community

Regards,

Xiaoxin Sheng

Thank you error disappear when i remove Tenant.ReadAll and Tenant.ReadWriteAll. It writted in documentation. https://learn.microsoft.com/en-us/rest/api/power-bi/admin/get-activity-events#permissions

VSmart_0-1679041097239.png

 

@VSmart 

I have tried this. Removed all the admin-consent required from API permissions. Still getting 401 error for activity events data.

 

  • I have added my Service Principal security group in "Service principals can call Fabric public APIs" & "Service principals can access read-only admin APIs" in PBI Service Admin portal,
  • Assigned Fabric Admin role to API security group

 

But still getting 401 error for activity events. I'm able to get the reports/datasets list though.

 

Let me know if I'm doing anything wrong.

Anonymous
Not applicable

HI @VSmart,

I'm glad to hear you find the root causing and share the resolution here, they will help others who faced the similar issues.
Regards,

Xiaoxin Sheng

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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