Authentication Issue with Power BI REST API - executeQueries

392 Views Asked by At

I am trying to execute a DAX on a Power BI dataset published in Premium workspace using below Power BI Rest API. https://api.powerbi.com/{api_version}/myorg/groups/{workspace_id}/datasets/{dataset_id}/executeQueries

And below is the full code

import requests

# Authentication
tenant_id = 'your-tenant-id'
client_id = 'your-client-id'
client_secret = 'your-client-secret'
workspace_id = 'your-workspace-id'
dataset_id = 'your-dataset-id'
api_version = 'v1.0'

# Get access token
auth_url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/token'
headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
}
data = {
    'grant_type': 'client_credentials',
    'client_id': client_id,
    'client_secret': client_secret,
    'resource': 'https://analysis.windows.net/powerbi/api'
}

response = requests.post(auth_url, headers=headers, data=data)
access_token = response.json()['access_token']

# Execute DAX query
api_endpoint = f'https://api.powerbi.com/{api_version}/myorg/groups/{workspace_id}/datasets/{dataset_id}/executeQueries'
headers = {
    'Authorization': f'Bearer {access_token}',
    'Content-Type': 'application/json'
}
query = {
    'queries': [
        {
            'query': 'EVALUATE SUMMARIZECOLUMNS("TotalSales", [TotalSales])'
        }
    ]
}

response = requests.post(api_endpoint, headers=headers, json=query)
result = response.json()

print(result)

Below is the error message, I am getting:

{'error': {'code': 'PowerBINotAuthorizedException', 'pbi.error': {'code': 'PowerBINotAuthorizedException', 'parameters': {}, 'details': [], 'exceptionCulprit': 1}}}

The client id and secret are correct, also the client id has admin access on the Power BI premium workspace. The specified DAX\Measure is correct as well. Not sure, what else permission is required for the SPN to evaluate the measure.

It already has Dataset.Read.All, Dataset.ReadWrite.All and Report.Read.All.

I am referring to below MS documentation and when I do a Try It and provide my own organization user id and password, it works, How can I make use of my own user and password\AAD to evaluate the DAX programmatically without a pop up screen asking for password.

https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/execute-queries?tryIt=true&source=docs#code-try-0

My userid has full permission as well on the Power BI Premium workspace.

0

There are 0 best solutions below