Forum Discussion
Access Power BI API with Python
- 9 years ago
jb007 wrote:
My requirement is to push real time data into Power BI using Python to first read from a database and then send the data inside a Streaming dataset in Power BI.
The first thing I want is to make a simple "get" call to Power BI.
The official documentation explains the processes of connecting to Power BI via the REST API for either a Client App or a Web App. However, I'm using Python - not sure if that is either a client app or a web app.
Anyway, I am able to get the accessToken using the adal library and the method .acquire_token_with_client_credentials, which asks for authority_uri, tenant, client_id and client_secret (notice this is not asking for username and password). By the way, I've also tried getting the accessToken with .acquire_token_with_username_password, but that didn't work.
Unfortunately, when I use the below code with the obtained accessToken, I get a response 403.#accessToken is received using the adal libary headers = {'Authorization': 'Bearer ' + accessToken, 'Content-Type': 'application/json'} read_datasets = requests.get('https://api.powerbi.com/v1.0/myorg/datasets', headers=headers) #shockingly, this will result in a response 403After reading other stackoverflow posts and looking at console apps, I believe the reason this doesn't work is because there is no user sign-in process.
This thread mentions that using Client Credentials is not enough (it is enough to get the accessToken, but not enough to use the APIs)
Not sure how to proceed, but what I need is perhaps a way to keep using this adal template that gives me the accessToken, and also to provide my username and password (in a silent way, i.e. via the scrip, without GUI), and together with the accessToken, to access the APIs.It is a web app. After registion, Use the client_id and client_secrect to get the accesstoken. What are the client_id and client_secrect in your case? 403 in your case most probably indicates a invalid accesstoken, when dedcoding the token at http://jwt.io, what is the scp(scope)? Normally you'd get access as below according to the premission checked when registering the app.
Techinically it is possible to get the access token without GUI interaction in a silent way, just call the POST API with grant_type=password. Then use the refreshtoken to get token afterwards. Note that it might violate the license compliance if other people use the this token to access the embedded reports. People who access the reports in Power BI service shall have their own accounts/tokens.
However for the first time, AFAIK, you'll always have the GUI consent page and accept it.
This might be helpful for anyone visiting; I have started working on a python client for the PowerBI API, feel free to use it!
https://github.com/cmberryau/pypowerbi
Right now I have added the functionality I require, but I'll be continuing to develop it as I'm now working full time on a python back-end project using PowerBI.
So far, ive added the functionality to:
- Use a standard ADAL token for authentication
- Pull reports
- Clone reports
- Delete reports
- Rebind reports
- Embed reports
- Pull datasets
- Push datasets
- Delete datasets
- Push rows
- Anonymous6 years agoNot applicable
Hi, I really like the look of this. Does it enable you to copy a dataset from one workspace to another?
Thanks
Ricky