【问题标题】:Node js App integration with power bi rest ApiNode js App 与 power bi rest Api 的集成
【发布时间】:2018-04-22 00:11:49
【问题描述】:

有没有办法在 node js 中使用 power bi rest API,我看了视频,Ran 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.js powerbi powerbi-embedded powerbi-datasource


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      @Jo Joy 你应该知道什么。

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

      这与这些公司决定他们在哪个项目中进行的优先级有关。

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

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

      【讨论】:

        【解决方案3】:

        如果您想达到同样的效果,请观看 Ran 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/把你的组ID放在这里/把你的报告ID放在这里

        【讨论】:

          【解决方案4】:

          我使用@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>
          

          【讨论】:

          • 如何在Android Webview中使用javascript显示embedUrl
          • @Jo Joy 你能创建一个示例仓库吗? ?我按照此github.com/microsoft/PowerBI-Developer-Samples/blob/master/… 关注.. 但无法查看报告。
          • 我在 Azure 中创建了我的 powerBI 实例。我确实在节点环境的 config.json 中添加了 clientID、workspaceID、reportID 等.. 但仍然面临问题.. 请帮助我
          猜你喜欢
          • 2016-06-06
          • 1970-01-01
          • 2020-07-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-07-21
          • 1970-01-01
          相关资源
          最近更新 更多