我正在尝试从基于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/)
我一直试图获取一个身份验证令牌。
C#站点生成一个auth-token,如果我粘贴到我的PHP站点,我可以用来加载Power BI表。但是,我不确定如何从PHP生成这个 - 可能是某个地方的卷曲请求,但我无法弄清楚我需要发送到哪里? [编辑:我一直在嗅探数据包,它似乎没有发出http请求来生成这个,所以也许我只需要知道如何自己生成它?]。 C#使用内置库(PowerBIToken)来完成此任务。
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);
}
}
我正在寻找一个简单的解决方案,如果可能的话,我可以遍历每一步而不是臃肿的库。
答案 0 :(得分:2)
令牌是JWT令牌,可以直接从PHP生成。
从这里包括JWT php类:https://github.com/firebase/php-jwt
要对API使用的调用进行身份验证:
$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);
要在浏览器中显示报告进行身份验证,请使用:
$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);
然后,您可以在浏览器中将其用作报告div上的powerbi-access-token
属性。
此外,如果它对任何人有帮助,这里是我用于API的Curl示例:
$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);
答案 1 :(得分:2)
这个doc页帮助我通过php / js获取令牌和嵌入代码 - 它逐步描述了每个请求和响应:
https://msdn.microsoft.com/en-us/library/azure/dn645542.aspx
授权代码授予流程图:
答案 2 :(得分:0)
您提供的C#代码段将Power BI报告集成到您的网站中。
As Power BI支持通过IFrame在您的网站上嵌入电源BI仪表板。 Embed the powerBi report in html iFrame
因此,要在PHP Web应用程序中实现此要求,您可以尝试利用Power BI for HTML / JavaScript。
或直接在PHP视图脚本中创建IFrame Dom。 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>
请参阅http://community.powerbi.com/t5/Developer/report-embed-problem/td-p/11490/highlight/true上Power BI社区的主题解决方案。