Power BI Embedded example using cURL and PHP
Recently Microsoft re-launched PowerBI Embedded as part of the PowerBI Premium offering. Instead of a pay-per-view pricing model Microsoft now offers dedicated capacity. There are three parts to get this new PowerBI Embedded working:
- Configuration in Azure (Register an app, activate the PowerBI Embedded capacity)
- Configuration in the PowerBI service (create a PowerBI app and activate the premium capacity)
- Development of a web application (requesting a token, requesting the embedded url and embed the actual report using javascript)
All above is explained in detail in the getting started guide. However, the last part is explained using a .NET example app. In this post we’ll show an alternative method using PHP and cURL.
Our basic PHP example uses two cURL requests: the first request will request a token, the second request uses this token to retrieve the embedded URL of the report. We’ll store the token and the embedded URL in PHP variables and use these in the JavaScript code to finally embed the report in an HTML div attribute.
Steps:
- Request the token
- Decode the result and store the token in a variable
- Request the embedded URL using the token variable
- Store the embedded URL in a variable
- Embed the report with Javascript using the two variables above
Step 1) Request the token:
/* Get oauth2 token using a POST request */
$curlPostToken = curl_init();
curl_setopt_array($curlPostToken, array(
CURLOPT_URL => "https://login.windows.net/common/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array(
grant_type => 'password',
scope => 'openid',
resource => 'https://analysis.windows.net/powerbi/api',
client_id => '', // Registered App ApplicationID
username => '', // for example john.doe@yourdomain.com
password => '' // Azure password for above user
)
));
$tokenResponse = curl_exec($curlPostToken);
$tokenError = curl_error($curlPostToken);
curl_close($curlPostToken);
Step 2) Decode the result and store the token in a variable:
// decode result, and store the access_token in $embeddedToken variable:
$tokenResult = json_decode($tokenResponse, true);
$token = $tokenResult["access_token"];
$embeddedToken = "Bearer " . ' ' . $token;
Step 3) Use this token variable in a GET request to get the embedded url:
/* Use the token to get an embedded URL using a GET request */
$curlGetUrl = curl_init();
curl_setopt_array($curlGetUrl, array(
CURLOPT_URL => "https://api.powerbi.com/v1.0/myorg/groups/YourGroupID/reports/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: $embeddedToken",
"Cache-Control: no-cache",
),
));
$embedResponse = curl_exec($curlGetUrl);
$embedError = curl_error($curlGetUrl);
curl_close($$curlGetUrl);
Step 4) Decode the result, retrieve the embedded URL and store this URL in a second variable:
if ($embedError) {
echo "cURL Error #:" . $embedError;
} else {
$embedResponse = json_decode($embedResponse, true);
$embedUrl = $embedResponse['value'][0]['embedUrl'];
}
5. In the last part we use the JavaScript code provided by Microsoft to embed the report:
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="scripts/powerbi.js"></script>
<div id="reportContainer"></div>
<script>
// Get models. models contains enums that can be used.
var models = window['powerbi-client'].models;
// Embed configuration used to describe the what and how to embed.
// This object is used when calling powerbi.embed.
// This also includes settings and options such as filters.
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
var embedConfiguration= {
type: 'report',
id: 'YourReportId', // the report ID
embedUrl: "<?php echo $embedUrl ?>",
accessToken: "<?php echo $token; ?>" ,
};
var $reportContainer = $('#reportContainer');
var report = powerbi.embed($reportContainer.get(0), embedConfiguration);
</script>
Juliano
Thanks for the tutorial, it helped a lot.
I have a question, when displaying the report appears the field: ... Export data, does it know in any way to remove this?
thank you
Lars Bouwens
Hi Juliano. Good to hear it was helpful 😉
Did you try to disable [export data] in PowerBI? I'm not sure if you can disable it specifically for Embedded. I've seen a question about this one but I'm not sure about the latest status (see: https://ideas.powerbi.com/forums/265200-power-bi-ideas/suggestions/16633324-disable-export-data-function-in-power-bi-embeded). I would advice you to post your question there or on the PowerBI forums! Good luck 🙂
Stan Evers
Nice blog Lars! Great to see you found an alternative to embed Power BI with PHP and cURL.
Lars Bouwens
Thanks Stan!
saanvi
a debt of gratitude is in order for sharing data exceptionally pleasant blog thank you lars!
MANO
Perfect!!!!!!!
Howard
Thanks Lars. Would you have any idea where to start if I constantly get wrong username/password when requesting the token in step 1? I've confirmed my details are good, tried it every possible way, but still get the error. If there is any place to even start troubleshooting this, it would be great! If not, no problem, thanks for helping by posting the code!
Lars Bouwens
Hi Howard, did you try to get the MS example working? See: https://docs.microsoft.com/en-us/power-bi/developer/embed-sample-for-customers . Perhaps you forgot some steps in Azure? You could also try to do some testing with Postman (https://www.getpostman.com/) to see if you are able to get a token back.
udhayavanan
thanks for the tutorial which made easy,
but i forget the missing option publish to web. without publish to web how i can get embed url. is there any option?
always i am getting this content is not available message.
Lars Bouwens
Publish to web is not the same as Power BI Embedded. Publish to web is visible for everyone,. Power BI Embedded reports are only accessible by users that are logged in to your application.
The embed URL for your report should be: https://app.powerbi.com/reportEmbed?reportId=5b218778-e7a5-4d73-8187-f10824047715. The reportId should be replaced by the reportid of your own report.
M
Hey! Thanks Man! Finally found a work around
Kenny
Thanks for sharing
Dallas
Hi Lars,
Thank you for sharing your expertise and knowledge and in a practical manner - Cheers!
I would like my customers to login to my app - and display an embedded report filtered on their data?
in pseudo - Would it happen by calling the onfilterchange at the onload time and then use a param from their profile?
Thanks again!
Lars Bouwens
You should use the customer username as a variable in your token. You can then also use this username as username() in your Power BI report. You could filter the data set dynamically based on the username.
Mohan
Hi can you provide code.how would i sent username to powerbi when i generate token,because two username field will be there.One is for login another is for RLS.How can i do for this
Mohan
how can i impemennt rls through this code anyone can help me
Lars Bouwens
I wrote this post in 2018, MS has changed a lot regarding Power BI Embedded since then. The token we request in step 1 works for this specific scenario. If you want to implement RLS you need to request a token for a specific user and use that token in the next steps.
A good starting point would be to try to generate a token using the following documentation:
https://docs.microsoft.com/nl-nl/rest/api/power-bi/embedtoken/generatetoken
Robert Davies
I have a question. I'm trying to implement this under the App Owns Data model for Power BI and it feels from the result that is 90%. I don't suypport you have any suggestions for App Owns Data?
Lars Bouwens
I am not sure if I understand your question. This scenario is the 'app owns data' model, because end users do not have to bring their own Power BI license.
Mathu
Hi Lars,
I have power Bi embedded ULR, if we use IFRAME the report comming but the problem was the URL was displaying in HTML iframe , is there any solution to use the embedded URL without IFRAME. Can we use PHP or javascript to embedded the report.
Thanks,
Mathu
Lars Bouwens
Can you share your code? I am not sure what you mean by the URL is showing in the iframe.
Hander Valencias
Hello, excellent article,
As of today, June 18, 2020, there is no easier way to perform a CULR consultation:
Example of invocation with CURL
curl --user "nameuser": "Password" --request GET 'https://test-website/api/v1/casos/abiertos?equipo=true'
Excuse my English
?
John Helmuth
Lars,
Thanks for the demonstration of accessing PowerBI embedded reports via the App Owns Data flow.
Do you know if it is possible to use Certificates in the Azure Key Store using a service principal (
https://docs.microsoft.com/en-us/power-bi/developer/embedded/embed-service-principal-certificate) with code similar to your demonstration code?
This is mostly to help me understand the underlying request/response flow, ideally there would be a PHP library that matches up with Microsoft's C# libraries so that their documentation is more helpful to those of us who are not using C#.