dsfsw1233
2016-04-18 12:34
浏览 375
已采纳

从PHP嵌入Power BI。 获取Azure身份验证令牌OAuth

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.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试从基于PHP的网站利用Power BI Embedded将非公共Power BI文档嵌入到 一个网页(在用户登录后面)。

这里有一个C#版本,我已经运行了: https://github.com/Azure-Samples/power-bi-embedded-integrate-report-into-web-app / 。 我实际上需要在PHP中复制它。

(另见 https://azure.microsoft.com/en-us/documentation/articles/power-bi-embedded-get-started/

我一直试图获取一个auth-token。

C#站点生成一个auth-token,如果我粘贴到我的PHP站点,我可以 用于加载Power BI表。 但是,我不确定如何从PHP生成这个 - 可能是某个地方的卷曲请求,但我无法弄清楚我需要发送到哪里? [编辑:我一直在嗅探数据包,它似乎没有发出http请求来生成这个,所以也许我只需要知道如何自己生成它?]。 C#使用内置库(PowerBIToken)来执行此操作。

  public async Task&lt; ActionResult&gt;  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 =&gt; r.Id == reportId); 
 var embedToken = PowerBIToken.CreateReportEmbedToken(  this.workspaceCollection,this.workspaceId,report.Id); 
 
 var viewModel = new ReportViewModel 
 {
 Report = report,
 AccessToken = embedToken.Generate(this.accessKey)
}; 
 \  n返回View(viewModel); 
} 
} 
   
 
 

我正在寻找一个简单的解决方案,我可以遍历每个步骤而不是膨胀的库 如果可能的话。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duandu5846 2016-04-21 12:15
    已采纳

    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);
    
    点赞 打赏 评论
  • dt1888 2016-04-19 06:21

    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.

    点赞 打赏 评论
  • dongpu4141 2016-06-13 23:37

    This doc page helped me getting token and embed codes by php/js - it describes every request and response step by step:

    https://msdn.microsoft.com/en-us/library/azure/dn645542.aspx

    Authorization Code Grant Flow Diagram:

    • Register the Application in Azure AD
    • Request an authorization code
    • Use the Authorization Code to Request an Access Token
    • Use the Access Token to Access the Resource
    • Use the Refresh Token to Request a New Access Token
    点赞 打赏 评论

相关推荐 更多相似问题