Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Power BI Access Token to refresh the Dataset through Python

I've developed a Power BI report that reads data from BLOB Storage, where new JSON files arrive every 2 minutes. To optimize performance, I've set up incremental refresh in Power BI. However, the refresh process is currently slow, taking about 15 minutes, even when only 1-2 small files (less than 5 MB each) are added to the BLOB storage. My requirement is to refresh the report every 5 minutes. To address this, I'm attempting to create a Python script that can be scheduled to run every 5 minutes. This script will detect changes in the BLOB storage and update the Power BI dataset accordingly. I'm facing challenges in connecting Power BI with Python, particularly in obtaining a Power BI Access Token. Can someone please provide me with detailed steps on how to obtain a Power BI Access Token?

Status: Investigating
Comments
Anonymous
Not applicable

Hi  @akhilesh2186 ,

 

To obtain a Power BI Access Token using Python, you will need to use the ADAL (Active Directory Authentication Library) module, which can be installed using pip install adal. You will also need to register an Azure AD app and grant it permissions to access the Power BI API. Here are the detailed steps:

  1. Register an Azure AD app by following the instructions here. Make sure to select Web as the application type and enter a valid Redirect URI (such as http://localhost:8000).
  2. After registering the app, go to the Overview section and copy the Application (client) ID and the Directory (tenant) ID. You will need these values later in your Python code.
  3. Go to the Certificates & secrets section and create a new client secret. Copy the value of the secret and store it securely. You will also need this value later in your Python code.
  4. Go to the API permissions section and click on Add a permission. Select Power BI Service from the list of APIs and then select Delegated permissions. Check the boxes for the permissions you need for your scenario, such as Dataset.ReadWrite.All and Report.ReadWrite.All. Click on Add permissions to save your changes.
  5. To grant admin consent for these permissions, click on Grant admin consent for {your organization}. You may need to sign in as an administrator to do this step.
  6. Now you are ready to write your Python code to obtain the Power BI Access Token. You can use the following code snippet as a template, replacing the values of client_id, client_secret, tenant_id, username, and password with your own credentials.
import adal

# Define constants
AUTHORITY_URL = 'https://login.windows.net/common'
RESOURCE = 'https://analysis.windows.net/powerbi/api'
CLIENT_ID = 'your-client-id-here'
CLIENT_SECRET = 'your-client-secret-here'
TENANT_ID = 'your-tenant-id-here'
USERNAME = 'your-username-here'
PASSWORD = 'your-password-here'

# Create authentication context
context = adal.AuthenticationContext(
    authority=f'{AUTHORITY_URL}/{TENANT_ID}',
    validate_authority=True,
    api_version=None
)

# Acquire token with username and password
token = context.acquire_token_with_username_password(
    resource=RESOURCE,
    username=USERNAME,
    password=PASSWORD,
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET
)

# Get access token from token dictionary
access_token = token['accessToken']

# Print access token
print(access_token)
  1. Run your Python code and verify that you get a valid access token printed in the console. You can use this token to make requests to the Power BI API, such as refreshing a dataset or updating a report.

I hope this helps you with your project. If you have any further questions, please let me know. 

 

Best regards.
Community Support Team_Caitlyn