gpt4 book ai didi

node.js - Node js App 与 power bi rest Api 的集成

转载 作者:IT老高 更新时间:2023-10-28 23:11:07 29 4
gpt4 key购买 nike

有没有办法在 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!

最佳答案

如果您想达到同样的效果,请观看 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放在这里

关于node.js - Node js App 与 power bi rest Api 的集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47194412/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com