Forum Discussion
How to use Power BI Rest API without GUI authentication (redirect uri)
- 10 years ago
You can use the user name and password flow that AAD supports. This 3rd party client library has an example: https://github.com/Vtek/PowerBI.Api.Client
Hi all,
with regards to the original subject, silent authenntication and also embedding PowerBI, i want to mention how i managed to get this to work and hopefully may help someone.
The way i managed to get silent authentication to work for embedding report items into a custom application was to use a master powerBI account (created via Office 365) and then setup a Native App in Azure Active Directory (AAD) and make the master account be the owner of the Native App and set the correct permissions in the app and generate a Key.
you then need to make a POST request to https://login.microsoftonline.com/common/oauth2/token with the following Keys and Values:
| KEY | VALUE |
| grant_type | password |
| scope | openid |
| resource | https://analysis.windows.net/powerbi/api |
| client_id | <the App ID for the Native App created in AAD> |
| client_secret | <the Key generated from the Native App created in AAD> |
| username | <Master PowerBI Account e.g. [email protected]> |
| password | <Password for the Master PowerBI account> |
You can test out your POST request by using the software application 'Postman'.
This will return back a the access token.
You then use the access token to pull back a Dashboard, Report or Tile - but bearing in mind that in PowerBI the Report / Dashboard must be bundelled into a PowerBI APP and owned by the Master Account - otherwise this won't work.
<html>
<script src="https://microsoft.github.io/PowerBI-JavaScript/demo/node_modules/jquery/dist/jquery.js"></script>
<script src="https://microsoft.github.io/PowerBI-JavaScript/demo/node_modules/powerbi-client/dist/powerbi.js"></script>
<script type="text/javascript">
window.onload = function () {
var embedConfiguration = {
type: 'dashboard', // dashboard
//change report embed url to dashboard embed url
accessToken: 'TOKEN GENERATED FROM POST REQUEST GOES HERE',
embedUrl: 'https://app.powerbi.com/dashboardEmbed?dashboardId=YOUR DASHBOARD ID - You can find this out by using http://docs.powerbi.apiary.io/ '
};
var $reportContainer = $('#dashboardContainer');
var report = powerbi.embed($reportContainer.get(0), embedConfiguration);
}
function reloadreport(){
var element = $('#dashboardContainer');
alert(element);
var report = powerbi.get(element);
report.reload().catch(error => {console.log(error) });
};
</script>
<div id="dashboardContainer"></div>
</html>
Hope this helps someone.
Shaheen K
Hi skaratela,
I tried this https://login.microsoftonline.com/common/oauth2/token with param in post method but getting below error-
"error": "invalid_grant",
"error_description": "AADSTS65001: The user or administrator has not consented to use the application with ID 'xxxxxxxxxxxxxxxxxxxx' named 'LSNTestAPP'. Send an interactive authorization request for this user and resource.\r\nTrace ID: ac36a442-1603-479b-aad5-b8c75b0f4300\r\nCorrelation ID: 4cd0780a-2184-46ab-8d91-8a83987c5e72\r\nTimestamp: 2017-12-22 06:14:36Z",
- skaratela8 years agoFrequent VisitorHi dharamgoyal it seems like perhaps the permissions in the Native Application in Azure haven't been set correctly. Also, are you using the same account for both the Native Application and also that same account when you are making the post request?
Finally, have you bundelled your dashboard into a power BI application and made the owner of that app the same account as above?
I know, it's so confusing.
Cheers