Publishing a report to workspace using Import Power Bi API endpoint in Python

19 Views Asked by At

I am trying to publish my report to my workspace using python. The idea was to use the Import API endpoint to publish my report. With my code currently, I am able to acquire access token and when calling other endpoints, the output comes out as expected. The error that I'm getting from using the Import endpoint is not well defined so I don't know what is wrong with it.

This is my code currently:

#Import necessary libraries

import msal
import requests
import json
import pandas as pd

 

#Set parameters

client_id = "<myclientid>" 
client_secret = "<myclientsecret>" 
tenant_name = "<mytenantname>"

authority_url = "https://login.microsoftonline.com/" + tenant_name
scope = ["https://analysis.windows.net/powerbi/api/.default"]


#Use MSAL to grab token

app = msal.ConfidentialClientApplication(client_id, authority=authority_url, client_credential=client_secret)
result = app.acquire_token_for_client(scopes=scope)

access_token = result['access_token']

 

pbix_file_path = r'C:\Users\abc\Desktop\publish_trial.pbix'
publish_url = 'https://api.powerbi.com/v1.0/myorg/groups/7e63865a-39fd-4a58-9274-cc6558a75a25/imports?datasetDispla...'

 

headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'multipart/form-data'
}

 

with open(pbix_file_path, 'rb') as pbix_file:
       pbix_content = pbix_file.read()

 

response = requests.post(publish_url, headers=headers, data=pbix_content)

response.text

I was expecting a 202 response. But I got a 400 response. And on printing the response text, this is what the error states:

{"error":{"code":"UnknownError","pbi.error":{"code":"UnknownError","parameters":{},"details":[],"exceptionCulprit":1}}}

Is there something I'm missing?

0

There are 0 best solutions below