节点js应用程序与电力集成restApi

有没有一种方法在node js中使用power bi rest API,我观看了video,Ran Breuer和Arina Hantsis在这里展示了这个演示,在我们的开发中,我想要实现相同但使用节点js的Power BI Embedded。环境我们不使用c#。 我find了Node SDK,但它说我们不再支持节点SDK, Node SDK

我必须改变开发结构从Node js到c#才能使用power bi Rest API!

如果你想达到同样的目标,那么Breuer和Arina Hantsis在那里展示video!

你可以使用这些代码…

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

** AppOwnData Powerembedded式报告**

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> 

最后享受

本地主机:4000 /报告/把你的组ID在这里/把你的报告ID在这里

我使用@Joyo Waseem代码,并成功embedded报告,然后我尝试embedded仪表板,它也可以工作,我决定在这里发布我的代码,所以任何试图embedded仪表板的人都可以使用这些代码。

embedded式仪表板使用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> 

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

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

这是公司决定在哪个项目中优先考虑的事项。

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

如果更新的API,你必须为你的尝试。 可能是你的人民呢。 我们作为共同将贡献。

他们不再支持Node SDK,但你试过了吗? 它可能还在工作。 你会想要某种types的SDK – 看起来并不是最简单的 API。