Skip to content

Instantly share code, notes, and snippets.

@aloncarmel
Last active July 23, 2020 17:02
Show Gist options
  • Save aloncarmel/952757a18f35daf76611e9ba2506da1e to your computer and use it in GitHub Desktop.
Save aloncarmel/952757a18f35daf76611e9ba2506da1e to your computer and use it in GitHub Desktop.
Embed a PowerBi Report using PowerBi JS library with AccessToken and EmbedToken
  1. You need to create an azure AD app and attach a registered app
  2. Create a secret key for your app
  3. Make sure your app has API permissions for powerbi
  4. Copy paste your app id (which is the client_id) and the secret key (which is the client_secret) to the file
  5. Surf to report.php?reportid=XXXXXREPORTID which you can take from your powerbi web enviroment and view your embedded report.
<?php
function getAccessToken() {
$curl = curl_init();
$data = array(
'client_id' => 'XXXXX',
'client_secret' => 'XXXX',
'grant_type' => "password",
'resource' => "https://analysis.windows.net/powerbi/api",
'username' => "XX@XXX.com",
'password' => "XXXX",
'scope'=> 'openid'
);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://login.microsoftonline.com/common/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=password&client_id=".$data['client_id']."&client_secret=".$data['client_secret']."&resource=".$data['resource']."&username=".$data['username']."&password=".$data['password']."&scope=".$data['scope']."",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
),
));
$response = curl_exec($curl);
curl_close($curl);
$response_array = json_decode($response);
$access_token = $response_array->access_token;
return $access_token;
}
function createEmbedTokenForReport($report_id) {
$power_bi_aad_token = getAccessToken();
$group_id = "XXXXXX";
$url = "https://api.powerbi.com/v1.0/myorg/groups/$group_id/reports/$report_id/GenerateToken";
$data = array(
"identities" => ""
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"{\"identities\":\"\",\"expiration\":\"2020-07-25T15:14:01Z\"}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer ".$power_bi_aad_token.""
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
echo createEmbedTokenForReport($_GET['reportid']);
?>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Power BI Embedded Demo</title>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
<style>
body,html {
padding:0px;
margin:0px;
height:100%;
width:100%;
}
iframe {
border:0px;
height:100%;
width:100%
}
</style>
</head>
<script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript" language="javascript" src="https://rawgit.com/Microsoft/PowerBI-JavaScript/master/dist/powerbi.min.js"></script> </script>
<div id="reportContainer" style="width: 100%; height: 100%;"></div>
<script>
$(document).ready(function () {
$.getJSON('/PBIgetEmbedToken.php?reportid=<?php echo $_GET['reportid'];?>',function(data){
var models = window['powerbi-client'].models;
var embedConfiguration = {
type: 'report',
id: '<?php echo $_GET['reportid'];?>',
embedUrl: 'https://app.powerbi.com/reportEmbed?reportId=<?php echo $_GET['reportid'];?>',
tokenType: models.TokenType.Embed,
accessToken: data.token
};
var $reportContainer = $('#reportContainer');
var report = powerbi.embed($reportContainer.get(0), embedConfiguration);
})
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment