Node js App 与 power bi rest 集成 Api

Node js App integration with power bi rest Api

有没有办法在 node js 中使用 power bi rest API,我看了视频,运行 Breuer 和 Arina Hantsis 在这里展示了演示,Setting up and Getting Started with Power BI Embedded 我想实现相同但使用 node js,在我们的开发环境中我们不使用 c#。 我找到了 Node SDK,但它说我们不再支持 Node SDK,Node SDK

我是否必须将开发结构从 Node js 更改为 c# 才能使用 power bi Rest API!!

他们不再支持Node SDK,但是你试过了吗?它可能仍在工作。您将需要某种 SDK - 似乎可以使用 not the easiest API。

@Jo Joy 你应该知道的是什么。

https://github.com/Microsoft/PowerBI-Node/issues/40

这些公司决定他们在哪个项目中的优先级。

他们对此反应很好。但就讨论而言,目前还没有这样的计划。您可以在 2017 年 2 月之前访问 api。

如果较新 api,您必须为您的 .可能是你吧。我们作为社区将做出贡献。

如果你想达到同样的效果,运行 Breuer 和 Arina Hantsis 在视频中演示了什么!

您可以使用这些代码...

阅读文档后,我想出了这个解决方案,花了我 5 天的时间才弄清楚,无论如何我在这里发帖,以便每个人都可以轻松访问代码。

**AppOwnData Power bi 嵌入式报告**

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);
    });
   }
   };

你的路由器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 或任何你喜欢的

<!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>

终于享受

localhost:4000/report/put 这里是你的组 id / 把你的报告 id 放在这里

我使用了@Joyo Waseem 代码并成功嵌入了报告,然后我尝试嵌入 Dashboard,它也能正常工作,我决定 post 我的代码在这里,这样任何试图嵌入 Dashboard 的人都可以使用这些代码。

使用 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等

 <!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>