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
Works for me