Node.js 应用程序与 Power BI REST API 集成

15

有没有使用Power BI Rest API在Node.js中的方法?我看了一个视频,Ran Breuer和Arina Hantsis在这里展示了演示:设置和入门Power BI嵌入式我想通过Node.js实现相同的效果,在我们的开发环境中,我们不使用c#。

我找到了Node SDK,但它说我们不再支持Node SDK, Node SDK

我是否必须从Node.js更改开发结构才能使用Power BI Rest API!!

4个回答

9

如果你想实现Ran Breuer和Arina Hantsis在视频中展示的内容,你可以使用以下代码...

阅读文档后,我得到了这个解决方案,花费了我5天的时间来找出来。无论如何,我在这里发布,以便每个人都可以轻松访问代码。

**AppOwnData Power bi embedded报告**

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/在此处放置你的小组编号/在此处放置你的报告编号


Necro,你知道是否可能只检索数据而不嵌入吗? - DJ22T

3
我使用了@Joyo Waseem的代码,并成功嵌入了报告,然后我尝试嵌入仪表盘,也成功了。我决定在这里发布我的代码,这样任何人想嵌入仪表盘都可以使用这些代码。
使用Power BI Rest 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 - AKASH WANGALWAR
@Jo Joy,请创建一个示例存储库。我按照这个链接的步骤进行操作:https://github.com/microsoft/PowerBI-Developer-Samples/blob/master/NodeJS/README.md,但是无法查看报告。 - Mr. Learner
我在Azure中创建了我的PowerBI实例。并且我已经在node环境的config.json文件中添加了clientID、workspaceID、reportID等信息,但仍然遇到问题。请帮助我解决这个问题。 - Mr. Learner

0

他们不再支持Node SDK,但你试过了吗?它可能仍然可以工作。你需要某种类型的SDK - 看起来这是最不容易使用的API。


0

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

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

这关乎公司决定在哪个项目中优先考虑。

他们对此非常了解。但就讨论而言,目前没有这样的计划。您可以在2017年2月之前访问API。

如果有更新的API,您需要自己尝试。也许您会遇到困难,但我们作为社区将会做出贡献。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接