Azure Power BI API Permission Activity.Read.All

Mike de Halpert 0 Reputation points
2024-10-03T08:52:41.35+00:00

Hi - I am trying to access the Power BI Get.ActivityEvents api using a service principal. I have registered the app, but my code keeps getting a 403 error.

I believe therefore it is a permissions error. I asked the handy Azure Copilot what I can do and it spat out the below instructions. See bullet point 5

User's image

The issue is that there is no "Activity.Read.All" under Delegated permissions, or under Application permissions.

So my question is two fold. How can I enable Activity.Read.All, or how can I give the App sufficient permissions to access the Get.ActivityEvents.

Below is the code I am using...

Thank you in advance

import requests
import json
from azure.identity import ClientSecretCredential
from datetime import date, timedelta

# Replace these variables with your own values
app = "PowerBI"

with open('config.json') as config_data:
    config = json.load(config_data)
    client_id = config[app]['client_id']
    client_secret = config[app]['client_secret']
    tenant_id = config[app]['tenant_id']

scope = 'https://graph.microsoft.com/.default'

# Define the date range for the activity events
activityDate = date.today() - timedelta(days=1)
activityDate = activityDate.strftime("%Y-%m-%d")

api_url = f"https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime='" + activityDate + "T00:00:00'&endDateTime='" + activityDate + "T23:59:59'"

# Authenticate and get the access token
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
token = credential.get_token(scope)
access_token = token.token

# Set the headers
headers = {
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {access_token}'
}

# Make the API request
response = requests.get(api_url, headers=headers)

# Check the response
if response.status_code == 200:
    activity_events = response.json()
    print(json.dumps(activity_events, indent=4))
else:
    print(f'Error: {response.status_code}')
    print(response.text)

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,055 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Navya 11,940 Reputation points Microsoft Vendor
    2024-10-09T02:10:50.9166667+00:00

    Hi @Mike de Halpert

    Thank you for posting this in Microsoft Q&A.

    I understand that you're having trouble accessing the Power BI Get Activity Events API using a service principal. Additionally, based on the instructions you provided, it looks like the issue might be related to permissions.

    Regarding your question about enabling Activity.Read.All, this permission is part of the Microsoft Graph API, not the Power BI API.

    You will need to grant the Tenant.Read.All or Tenant.ReadWrite.All API permissions for your application.

    User's image

    Hope this helps. Do let us know if you any further queries.

    Thanks,

    Navya.

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.