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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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))

 

 

 

 

 

 

3 REPLIES 3
v-shex-msft
Community Support
Community Support

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

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

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

 

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

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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