Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js App integration with power bi rest Api

Is there a way using power bi rest API in node js, I watched video ,Ran Breuer and Arina Hantsis were showing the demo here,Setting up and Getting Started with Power BI Embedded I want to achieve same but using node js, in our development environment we do not use c#. I found the Node SDK but it saying we no longer support node SDK,Node SDK

Do I have to change development structure from Node js to c# in order to use power bi Rest API!!

like image 296
Jo Joy Avatar asked Nov 09 '17 05:11

Jo Joy


People also ask

Can Power BI connect to REST API?

In the desktop Power BI application, you can get data by clicking the “Get data” button. Then you can choose the “Web” option, and provide the endpoint URL of the REST API you want to pull the data from. You can now import the data to Power BI!

Can we create REST API using node js?

Node. js can be used to create a variety of applications, including Command-Line Applications, Web Applications, Real-time Chat Applications, REST API Servers, and so on.

Is node js GOOD FOR REST API?

Node. js is mature and it powers services for some huge companies like LinkedIn, Walmart, eBay, PayPal, Yahoo and others. Finally, I'd like to say that our experience also proves that Node. js is a great choice to construct REST API.

Which API is used in Node js?

Node-API is a toolkit introduced in Node 8.0. 0 that acts as an intermediary between C/C++ code and the Node JavaScript engine. It permits C/C++ code to access, create, and manipulate JavaScript objects as if they were created by JavaScript code.


Video Answer


2 Answers

If you want to achieve same, what Ran Breuer and Arina Hantsis demonstrate in there video!

you can use these codes...

after reading the documentation, I come up with this solution it took me 5 days to figure out, anyway I am posting here so everyone can have easy access to codes.

**AppOwnData Power bi embedded reports **

Controller.js

 const request = require('request');

 const getAccessToken = function () {

return new Promise(function (resolve, reject) {

    const url = 'https://login.microsoftonline.com/common/oauth2/token';

    const username = ''; // Username of PowerBI "pro" account - stored in config
    const password = ''; // Password of PowerBI "pro" account - stored in config
    const clientId = ''; // Applicaton ID of app registered via Azure Active Directory - stored in config

    const headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    };

    const formData = {
        grant_type: 'password',
        client_id: clientId,
        resource: 'https://analysis.windows.net/powerbi/api',
        scope: 'openid',
        username: username,
        password: password
    };

    request.post({
        url: url,
        form: formData,
        headers: headers
    }, function (err, result, body) {
        if (err) return reject(err);
        const bodyObj = JSON.parse(body);
        resolve(bodyObj.access_token);
    });
   });
  };

  const getReportEmbedToken = function (accessToken, groupId, reportId) {

return new Promise(function (resolve, reject) {

    const url = 'https://api.powerbi.com/v1.0/myorg/groups/' + groupId + '/reports/' + reportId + '/GenerateToken';

    const headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + accessToken
    };

    const formData = {
        'accessLevel': 'view'
    };

    request.post({
        url: url,
        form: formData,
        headers: headers

    }, function (err, result, body) {
        if (err) return reject(err);
        const bodyObj = JSON.parse(body);
        resolve(bodyObj.token);
    });
});
};


   module.exports = {
embedReport: function (req, res) {
    getAccessToken().then(function (accessToken) {
        getReportEmbedToken(accessToken, req.params.groupId, req.params.reportId).then(function (embedToken) {
            res.render('index', {
                reportId: req.params.dashboardId,
                embedToken,
                embedUrl: 'https://app.powerbi.com/reportEmbed?reportId=' + req.params.reportId + '&groupId=' + req.params.groupId
            });
        }).catch(function (err) {
            res.send(500, err);
        });
    }).catch(function (err) {
        res.send(500, err);
    });
   }
   };

Your router index.js

   const express = require('express'),
   router = express.Router(),
   mainCtrl = require('../controllers/MainController');
  router.get('/report/:groupId/:reportId', mainCtrl.embedReport);
  module.exports = router;

index.ejs or what ever you like

<!DOCTYPE html>
   <html>

    <head>
  <title>Node.js PowerBI Embed</title>
  <link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.min.css" />
  <style>
    html,
    body {
  height: 100%;
   }

.fill {
  min-height: 100%;
  height: 100%;
  box-sizing: border-box;
}

#reportContainer {
  height: 100%;
  min-height: 100%;
  display: block;
}
</style>
</head>
 <body>
<div class="container-fluid fill">
<div id="reportContainer"></div>
</div>
<script src="/jquery/dist/jquery.min.js"></script>
<script src="/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="/powerbi-client/dist/powerbi.js"></script>
<script>
const models = window['powerbi-client'].models;
const config = {
  type: 'report',
  tokenType: models.TokenType.Embed,
  accessToken: '<%- embedToken %>',
  embedUrl: '<%- embedUrl %>',
  id: '<%- reportId %>'
    };
   // Get a reference to the embedded dashboard HTML element 
   const reportContainer = $('#reportContainer')[0];
  // Embed the dashboard and display it within the div container. 
   powerbi.embed(reportContainer, config);
 </script>
 </body>

</html>

Finally Enjoy

localhost:4000/report/put your group id here / put you report id here

like image 188
Joyo Waseem Avatar answered Nov 03 '22 00:11

Joyo Waseem


I used @Joyo Waseem codes and successfully embedded the report, then I give a try to embed Dashboard and it works too, I decided to post my codes here so anyone who tries to embed Dashboard can use these codes.

Embedded Dashboard using power bi Res API

Controler.js

  const request = require('request');

   const getAccessToken = function () {

   return new Promise(function (resolve, reject) {

    const url = 'https://login.microsoftonline.com/common/oauth2/token';

    const username = ''; // Username of PowerBI "pro" account - stored in config
    const password = ''; // Password of PowerBI "pro" account - stored in config
    const clientId = ''; // Applicaton ID of app registered via Azure Active Directory - stored in config

    const headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    };

    const formData = {
        grant_type: 'password',
        client_id: clientId,
        resource: 'https://analysis.windows.net/powerbi/api',
        scope: 'openid',
        username: username,
        password: password
    };

    request.post({
        url: url,
        form: formData,
        headers: headers
    }, function (err, result, body) {
        if (err) return reject(err);
        const bodyObj = JSON.parse(body);
        resolve(bodyObj.access_token);
    });
   });
   };

 const getEmbedToken = function (accessToken, groupId, dashboardId) {

return new Promise(function (resolve, reject) {

    const url = 'https://api.powerbi.com/v1.0/myorg/groups/' + groupId + '/dashboards/' + dashboardId + '/GenerateToken';

    const headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + accessToken
    }; 

    const formData = {
        'accessLevel': 'View'
    };

    request.post({
        url: url,
        form: formData,
        headers: headers

    }, function (err, result, body) {
        if (err) return reject(err);
        const bodyObj = JSON.parse(body);
        resolve(bodyObj.token);
    });
    });
     };


  module.exports = {
prepareView: function(req, res) {
    getAccessToken().then(function(accessToken) {
        console.log(req.params.groupId);
        getEmbedToken(accessToken, req.params.groupId, req.params.dashboardId).then(function(embedToken) {
            res.render('index', {
                dashboardId: req.params.dashboardId,
                embedToken,
                embedUrl: 'https://app.powerbi.com/dashboardEmbed?dashboardId=' + req.params.dashboardId + '&groupId=' + req.params.groupId
            });
        });
    });
}
};

index.js

  const express = require('express'),
  router = express.Router(),
  mainCtrl = require('../controllers/MainController');
  router.get('/dashboard/:groupId/:dashboardId', mainCtrl.prepareView);
  module.exports = router;

index.ejs etc.

 <!DOCTYPE html>
    <html>
      <head>
    <title>Node.js PowerBI Embed</title>
    <link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.min.css" />
  <style>
  html,
   body {
  height: 100%;
   }

.fill {
  min-height: 100%;
  height: 100%;
  box-sizing: border-box;
}

  #dashboardContainer {
   height: 100%;
   min-height: 100%;
   display: block;
   }
 </style>
 </head>
<body>
  <div class="container-fluid fill">
 <div id="dashboardContainer"></div>
 </div>
 <script src="/jquery/dist/jquery.min.js"></script>
 <script src="/bootstrap/dist/js/bootstrap.min.js"></script>
 <script src="/powerbi-client/dist/powerbi.js"></script>
 <script>
 const models = window['powerbi-client'].models;
 const config = {
  type: 'dashboard',
  tokenType: models.TokenType.Embed,
  accessToken: '<%- embedToken %>',
  embedUrl: '<%- embedUrl %>',
  id: '<%- dashboardId %>'
  };
// Get a reference to the embedded dashboard HTML element 
   const dashboardContainer = $('#dashboardContainer')[0];
// Embed the dashboard and display it within the div container. 
   powerbi.embed(dashboardContainer, config);
   </script>
   </body>
   </html>
like image 26
Jo Joy Avatar answered Nov 03 '22 01:11

Jo Joy