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

从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.

  • 写回答

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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀