13

有没有办法在节点 js 中使用 power bi rest API,我观看了视频,Ran Breuer 和 Arina Hantsis 在这里展示了演示,Power BI Embedded 的设置和入门我想在我们的开发中实现相同但使用节点 js环境我们不使用c#。我找到了 Node SDK,但它说我们不再支持 Node SDK、Node SDK

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

4

4 回答 4

8

如果您想达到同样的效果,请观看 Ran Breuer 和 Arina Hantsis 在视频中展示的内容!

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

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

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

控制器.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/把你的组ID放在这里/把你的报告ID放在这里

于 2017-11-18T12:28:37.930 回答
3

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

使用 power bi Res API 的嵌入式仪表板

控制器.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>
于 2017-11-19T12:53:34.240 回答
0

@Jo Joy 你应该知道什么。

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

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

他们可以很好地回应这一点。但就讨论而言,没有这样的计划这样做。您可以在 2017 年 2 月之前访问 api。

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

于 2017-11-14T12:37:42.043 回答
-1

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

于 2017-11-14T12:16:37.467 回答