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)

Azure App Configuration
Azure App Configuration
An Azure service that provides hosted, universal storage for Azure app configurations.
229 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,743 questions
{count} votes

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.