Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
Dear Community,
I create an access token in Python using the below code:
import adal
import requests
application_id = "9f9439fd-4150-46c4-bxxxxxxx"
application_secret = "xxxx"
resource_url = "https://analysis.windows.net/powerbi/api"
authority_url = 'https://login.windows.net/common'
# AUTHORITY = "https://login.microsoftonline.com/513294a0-3e20-41b2-a970-6d30bf1546fa"
# first you need to authenticate using adal
context = adal.AuthenticationContext(authority=authority_url,
validate_authority=True,
api_version=None)
token = context.acquire_token_with_client_credentials(resource_url, application_id, application_secret)
accessToken = token['accessToken']
The code works fine! I get a proper access token. But if I do some REST API calls like this:
#get datasets
url = "https://api.powerbi.com/v1.0/myorg/datasets"
headers = {
'authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
}
r = requests.get(
url=url,
headers=headers
)
print(r)
I receive the below error:
<Response [401]>
which is an authentication error.
In Azure active directory I have created a multi-tenant application, so I use that client_id and secret.
I believe there is somewhere in Azure AD, something should be fixed.
Does anyone have insight into why this token is not accepted?? (If I generate a token in Powershell, the API returns <Response [200]>, so I'm confused)
Regards,
Majid
Hi @Anonymous ,
I think you are using the SPN (Service Principal) Approach which needs to provide client id and client secret.
Changes you have to made.
E.g, you have to call https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets
The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!
| User | Count |
|---|---|
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 |
| User | Count |
|---|---|
| 9 | |
| 6 | |
| 4 | |
| 4 | |
| 3 |