Forum Discussion

capradeep1984's avatar
capradeep1984
New Member
3 years ago

Power BI Dataset refresh through REST Api using Python

Hi Team,

 

I am unable to use Python to refresh the Power Bi using REST API. Need quick help.

 

My code snippet is as below:

import requests
import json
import base64

# Set the API endpoint URL
url = "https://api.powerbi.com/v1.0/myorg/datasets/{dataset}/refreshes"

# Set the user ID and password
user_id = "my email ID"
password = "my password"

# Encode the user ID and password as base64
auth = base64.b64encode(f"{user_id}:{password}".encode()).decode()

# Set the request headers with the Content-Type and Authorization
headers = {
"Content-Type": "application/json",
"Authorization": f"Basic {auth}"
}

# Set the request payload
payload = {
"notifyOption": "NoNotification"
}

# Send the POST request with the headers and payload
response = requests.post(url, headers=headers, data=json.dumps(payload))

# Print the response
print(response)

12 Replies

  • Hi. The way to connect to PowerBi Rest API is using an App Registered at Azure Active Directory. You won't be able to get a valid token without an app id. You will be able to use a user account or service principal as login with the app registered. Once the app is created it will require permission for read and write dataset in order to use the refresh action. The first part of the following post shows how to do it with Service Principal ( generating a key to login with that instead of a user ). Read more: https://blog.ladataweb.com.ar/post/642023841877671936/azuredatafactory-actualizar-powerbi-con

    You can use libraries for this. Like: https://pypi.org/project/SimplePBI/

    That might guide you a little on how to write the code or register the app on azure to start working.

    I hope that helps,

    • ibarrau's avatar
      ibarrau
      Super User

      Hi. What's not working? I have just tried pip install and it's modules and are working. It's simplePbi with a P there. There is a typo there.

      Regards

      • capradeep84's avatar
        capradeep84
        Regular Visitor

        Yes. I I was able to install the package. Let me explore now

  • capradeep84's avatar
    capradeep84
    Regular Visitor

    I am getting option to enable read write datasets in delegated power bi service under tenant