Node js App与Power Bi Rest Api的集成 [英] Node js App integration with power bi rest Api

查看:109
本文介绍了Node js App与Power Bi Rest Api的集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在node js中使用power bi rest API,我观看了视频,Ran Breuer和Arina Hantsis在此处演示了该演示,通过Power BI Embedded进行设置和入门我想实现相同的目标,但是要使用Node js,我们的开发环境中我们不使用c#。
我找到了Node SDK,但是它说我们不再支持Node SDK, Node SDK

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

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

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

推荐答案

如果您要实现相同的目标,Ran Breuer和Arina Hantsis会在视频中进行演示!

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

您可以使用以下代码...

you can use these codes...

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

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嵌入式报告**

**AppOwnData Power bi embedded reports **

Controller.js

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

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或任何您喜欢的东西

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>

最后享受

localhost:4000 /在此处报告/输入您的组ID /在此处报告您的报告

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

这篇关于Node js App与Power Bi Rest Api的集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆