Power BI Embedded from PHP. Obtain an Azure Authentication token OAuth

power bi embedded playground
power bi embed token
power bi embedded website example
embed power bi dashboard javascript
power bi embedded print
power bi embed visual
powerbi.embed javascript
power bi embedded tutorial

I'm trying to utilise Power BI Embedded from a PHP based website to embed a non-public Power BI document into a web page (behind a user login).

There is a C# version here that I have got running: https://github.com/Azure-Samples/power-bi-embedded-integrate-report-into-web-app/. I effectively need to replicate this in PHP).

(also see https://azure.microsoft.com/en-us/documentation/articles/power-bi-embedded-get-started/)

I'm stuck trying to obtain a auth-token.

The C# site generates an auth-token that if I paste into my PHP site, I can use to load the Power BI sheet. However, I'm not sure how to generate this from PHP - presumably a curl request somewhere, but I can't work out what I need to send where? [Edit: I've been sniffing packets and it doesn't seem to make an http request to generate this, so perhaps I just need to know how to generate it myself?]. The C# is using a built in library (PowerBIToken) to do this.

public async Task<ActionResult> Report(string reportId)
    {


        var devToken = PowerBIToken.CreateDevToken(this.workspaceCollection, this.workspaceId);
        using (var client = this.CreatePowerBIClient(devToken))
        {
            var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);
            var report = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);
            var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id);

            var viewModel = new ReportViewModel
            {
                Report = report,
                AccessToken = embedToken.Generate(this.accessKey)
            };

            return View(viewModel);
        }
    }

I'm looking for a simple solution where I can walk through each step rather than a bloated library if possible.

After some investigation I worked this one out myself.

The token is a JWT token, which can be generated directly from PHP.

Include the JWT php class from here: https://github.com/firebase/php-jwt

To authenticate for calls to the API use:

$key = "<your Azure access key>";
$payload = array(
    "ver" => "0.1.0",
    "type" => "dev",
    "wcn" => "<your workspace collection name>",
    "wid" => "<your workspace ID>",
    "iss" => "PowerBISDK",
    "aud" => "https://analysis.windows.net/powerbi/api",
    "exp" => time()+60*60,
    "nbf" => time()
);
$token = JWT::encode($payload,$key);

And to authenticate for displaying a report in the browser use:

$key = "<your Azure access key>";
$payload = array(
    "ver" => "0.1.0",
    "type" => "embed",
    "wcn" => "<your workspace collection name>",
    "wid" => "<your workspace ID>",
    "rid" => "<your reportID (as uploaded to your collection)>",
    "iss" => "PowerBISDK",
    "aud" => "https://analysis.windows.net/powerbi/api",
    "exp" => time()+60*60,
    "nbf" => time()
);
$token = JWT::encode($payload,$key);

You can then use this as the powerbi-access-token attribute on your report div in the browser.

Also, in case it helps anyone, here's an example of the Curl I use for the API:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.powerbi.com/beta/collections/<your workspace collection name>/workspaces/<your workspace ID>/reports");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Might be required for https
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Authorization: AppToken " . $token
));
$response_json = curl_exec($ch);
curl_close($ch);

$response_data = json_decode($response,true);

Power BI Embedded from PHP. Obtain an Azure Authentication , PHP. Obtain an Azure Authentication token OAuth - php. I'm trying to utilise Power BI Embedded from a PHP based website to embed a non-public Power BI​  In step 1, you registered a client app in Azure AD. In this step, you get an authentication access token. Power BI apps are integrated with Azure Active Directory to provide your app with secure sign in and authorization. Your app uses a token to authenticate to Azure AD and gain access to Power BI resources.

Easily get an access token for the Power BI Embedded Playground , You need an Embed Token for the Power BI Embedded Playground, but you don'​t want to Duration: 8:22 Posted: Apr 4, 2019 This article shows how you can authenticate users in your Power BI application and retrieve an access token to use with the Power BI REST API. Before your app calls the REST API, you need to get an Azure Active Directory (Azure AD) authentication access token. Your app uses a token to get access to Power BI dashboards, tiles, and reports.

The C# code snippet you provide seams to integrate Power BI reports into your web site.

As Power BI Supports for embed power BI dashboard on your website via IFrame. Embed the powerBi report in html iFrame.

So to implement this requirement in PHP web app, you can try to leverage Power BI for HTML / JavaScript.

Or directly create IFrame Dom attributed in your PHP view script. E.G.

<html lang="en">
<head>
    <script type="text/javascript">
        // post the auth token to the iFrame.
        function postActionLoadReport() {

            // get the access token.
            accessToken = '<?php echo $accessToken;?>';

            // return if no a
            if ("" === accessToken)
                return;

            // construct the push message structure
            // this structure also supports setting the reportId, groupId, height, and width.
            // when using a report in a group, you must provide the groupId on the iFrame SRC
            var m = { action: "loadReport", accessToken: accessToken};
            message = JSON.stringify(m);

            // push the message.
            iframe = document.getElementById('iFrameEmbedReport');
            iframe.contentWindow.postMessage(message, "*");;
        }
     </script>
</head>
<body>
    <div>
        <p><b>Embedded Report</b></p>
        <table> <tr>
                    <td>
                        <iframe id="iFrameEmbedReport" src="<?php echo $reportURI;?>" onload="postActionLoadReport()" height="768px" width="1024px" frameborder="1" seamless></iframe>
                    </td>
                </tr>
        </table>
    </div>
</body>

Please refer to the solution of the thread on Power BI community at http://community.powerbi.com/t5/Developer/report-embed-problem/td-p/11490/highlight/true.

Get an authentication access token, Walk through to push data - Get an authentication access token. Power BI developer documentation · Tutorial: Embed Power BI in an app Power BI apps are integrated with Azure Active Directory to provide your app with secure "​https://analysis.windows.net/powerbi/api"; //OAuth2 authority Uri string  Getting access key in Azure portal is the only approach to get the proper "key" in that being deprecated Power BI Embedded. If you don't like it, you can consider the replacement Embedding with non-Power BI users (app owns data) , just follow the tutorial.

Power BI Embedded example using cURL and PHP, 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 We'll store the token and the embedded URL in PHP variables and use these in the Get oauth2 token using a POST request */. Hi everyone . I'm making use of app-only authentication (the app owns data method), I've already set up a new group and created a service principle, as well as enabled the "Allow service principle to use Power BI Apps" option in Power BI, where I added the group to as well.

App only authentication - oAuth2 token request, My question is how do I perform the oauth token request with the app id and so can't run the c# examples, I'm yet to see a simple Curl / php example of authenticating. I'd like to mention again the end result here is for me to get embed In Azure AD when I navigate to this group and click on members,  Power BI Embedded continues to use Azure AD for master user (a designated Power BI Pro licensed user) authentication, or with service principal for authenticating the application inside Power BI. An ISV can implement their own authentication and authorization for their applications.

Trying to generate embed token via Javascript auth flow : PowerBI, My ultimate goal here is to generate an embed token to embed a report as part I'm trying to replicate the "Access token for non-Power BI users (app owns data)" .com/en-us/documentation/powerbi-developer-get-azuread-access-token/ var clientId = // Applicaton ID of app registered via Azure Active Directory - stored​  I've followed this example to get an OAuth token, add it to the header in a call to our API service, and have it working from Power BI Desktop. However, I can't seem to configure automated refresh from the Power BI service portal for this data source. I recieve the message "Your data source can't be refreshed because the credentials are invalid

Comments
  • Do you want to call the Power BI Embedded API at msdn.microsoft.com/en-us/library/mt712303.aspx from PHP, or you want to integrate Power BI reports into your PHP web?
  • @Gary Liu potentially both. The API you linked to requires an app token, and that's the bit I can't figure out how to generate from PHP.
  • Hello Gary, thank you for the thorough reply. Am I right in thinking that this is only for reports that have been published to web using the "file" > "publish to web" option on the Power BI site? Unfortunately this means that the reports are public which unfortunately is not appropriate for my use case. powerbi.microsoft.com/en-us/documentation/…
  • To clarify, are you looking for the solution in PHP to implement the functionality shown at powerbi.microsoft.com/en-us/documentation/…?
  • Yes, I think so - thanks Gary, this could be what I need: powerbi.microsoft.com/en-us/documentation/… I'll take a look
  • having looked further, I'm not sure this is what I need. I've reworded the initial question to hopefully make things clearer.