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.

Reply
Anonymous
Not applicable

[Python] Get AccessToken POST Request + Multi-factor Auth

Hello,

 

I'm trying to get an AccessToken from https://login.microsoftonline.com/common/oauth2/token but I have two problems

 

  • My user for security reason have MFA enabled, and cannot be disabled.
  • The code I have found doesn't work only using clientID and ClientSecret (Source)

The code below generates an access code, but the POST request retrieves 401 status code.

 

I can disable MFA and enter User and Password and would generate a valid access token, but that generates a massive security problem, besides implies that I would need to have a "permanent" account that would only have access to PBI for doing refreshes - basically a flaw into the system of APIs.

 

import adal
import requests
from msrestazure.azure_active_directory import AADTokenCredentials

authority_host_uri = 'https://login.microsoftonline.com'
tenant = 'tenant'
authority_uri = authority_host_uri + '/' + tenant
resource_uri = 'https://analysis.windows.net/powerbi/api'
client_id = 'clientid'
client_secret = 'clientsecret'
context = adal.AuthenticationContext(authority_uri, api_version=None)
mgmt_token = context.acquire_token_with_client_credentials(resource_uri, client_id, client_secret)
credentials = AADTokenCredentials(mgmt_token, client_id)


headers = {'Authorization': 'Bearer ' + mgmt_token['accessToken'], 'Content-Type': 'application/json'}
url = 'https://api.powerbi.com/v1.0/myorg/groups/me/datasets/datasetid'

r= requests.post(url, headers=headers)
print(r)

 

1 REPLY 1
Jayendran
Solution Sage
Solution Sage

Hi ,

 

You don't need to disable the MFA for your account. Instead you can overcome the issue with using SPN

https://docs.microsoft.com/en-us/power-bi/developer/embed-service-principal

 

Just upgrade your workspace to v2.

 

Issue resolved !

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.