[英]Power BI Embedded from PHP. Obtain an Azure Authentication token OAuth
我正在嘗試從基於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/zh-cn/documentation/articles/power-bi-embedded-get-started/ )
我一直在嘗試獲取身份驗證令牌。
C#站點生成一個身份驗證令牌,如果我將其粘貼到PHP站點中,則可以用來加載Power BI工作表。 但是,我不確定如何從PHP生成此消息-大概是某個地方的curl請求,但是我無法確定需要將其發送到哪里? [編輯:我一直在嗅數據包,似乎並沒有發出生成此請求的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);
}
}
我正在尋找一個簡單的解決方案,在該解決方案中,我可以逐步執行每個步驟,而不是如果可能的話,可以使用膨脹的庫。
經過一番調查,我自己解決了這個問題。
該令牌是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);
該文檔頁面幫助我通過php / js獲取令牌和嵌入代碼-逐步描述了每個請求和響應:
https://msdn.microsoft.com/zh-CN/library/azure/dn645542.aspx
授權碼授予流程圖:
您提供的C#代碼段將Power BI報表集成到您的網站中。
As Power BI支持通過IFrame在您的網站上嵌入Power BI儀表板。 將powerBi報告嵌入html iFrame中 。
因此,要在PHP Web應用程序中實現此要求,您可以嘗試將Power BI用於HTML / JavaScript 。
或直接在您的PHP視圖腳本中創建IFrame Dom。 例如
<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社區上的線程解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.