Embedding Power BI dashboards in a web application is a powerful way to share insights securely with users. In this tutorial, I'll guide you through the steps to set up a basic solution using Express for the backend and React for the frontend, while covering essential configurations in Azure and Power BI.
1. Configurations in Azure
To embed Power BI reports, you need to register an app in Azure Active Directory:
-
Register an Application:
- Go to the Azure portal → Azure Active Directory → App registrations → New registration.
- Enter a name (e.g., "PowerBIEmbedApp") and set the Redirect URI to your frontend's base URL.
-
API Permissions:
- Add permissions for Power BI Service (Delegated and Application).
- Grant admin consent for the permissions.
-
Client Secret:
- Create a new client secret under Certificates & secrets.
- Note down the Application (client) ID, Directory (tenant) ID, and Client Secret.
2. Configurations in Power BI
-
Enable Embed Token Generation:
- Log in to the Power BI Service → Admin Portal → Tenant Settings.
- Enable "Allow service principals to use Power BI APIs".
-
Assign Workspace Access:
- Add the service principal (registered app) to the Power BI workspace as an Admin or Member.
-
Obtain Dashboard/Report IDs:
- Note down the workspace, dashboard, and report IDs of the content you want to embed.
3. Backend: Express Setup
The backend is responsible for generating an embedded token for the dashboard.
Install dependencies:
npm install express axios dotenv
Backend Code:
const express = require('express'); const axios = require('axios'); const dotenv = require('dotenv'); dotenv.config(); const app = express(); app.use(express.json()); // Environment variables from .env const { TENANT_ID, CLIENT_ID, CLIENT_SECRET, WORKSPACE_ID, REPORT_ID, } = process.env; const POWER_BI_AUTH_URL = `https://login.microsoftonline.com/${TENANT_ID}/oauth2/v2.0/token`; app.get('/api/getEmbedToken', async (req, res) => { try { // Get Access Token const authResponse = await axios.post(POWER_BI_AUTH_URL, new URLSearchParams({ grant_type: 'client_credentials', client_id: CLIENT_ID, client_secret: CLIENT_SECRET, scope: 'https://analysis.windows.net/powerbi/api/.default', })); const accessToken = authResponse.data.access_token; // Get Embed Token const embedResponse = await axios.post( `https://api.powerbi.com/v1.0/myorg/groups/${WORKSPACE_ID}/reports/${REPORT_ID}/GenerateToken`, { accessLevel: 'View' }, { headers: { Authorization: `Bearer ${accessToken}` } } ); res.json(embedResponse.data); } catch (error) { console.error(error.message); res.status(500).send('Error generating embed token'); } }); const PORT = 5000; app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
4. Frontend: React Setup
Install dependencies:
npm install react powerbi-client axios
Frontend Code:
import React, { useEffect, useState } from 'react'; import axios from 'axios'; import { PowerBIEmbed } from 'powerbi-client-react'; const Dashboard = () => { const [embedConfig, setEmbedConfig] = useState(null); useEffect(() => { const fetchEmbedToken = async () => { try { const response = await axios.get('http://localhost:5000/api/getEmbedToken'); setEmbedConfig({ type: 'report', tokenType: 1, accessToken: response.data.token, embedUrl: response.data.embedUrl, settings: { panes: { filters: { visible: false } } }, }); } catch (error) { console.error('Error fetching embed token:', error); } }; fetchEmbedToken(); }, []); if (!embedConfig) return <div>Loading...</div>; return ( <powerbiembed embedconfig="{embedConfig}" cssclassname="dashboard-embed"></powerbiembed> ); }; export default Dashboard;
5. Final Steps
- Run the backend: node server.js.
- Run the frontend: npm start (assuming Create React App or similar setup).
- Visit your frontend application to see the embedded Power BI dashboard.
Takeaways
This setup provides a secure and simple way to embed Power BI dashboards. For production, consider:
- Storing sensitive environment variables securely (e.g., using Azure Key Vault).
- Implementing better error handling and logging.
- Securing API routes with authentication/authorization.
For more information, I invite you to check out how I implemented this in a real-world scenario in this article. Additionally, you can find the implementation using Next.js in this repository, adapted to a modern and scalable stack.
Feel free to customize this solution based on your project's needs!
Thanks for reading !! ??
The above is the detailed content of Embedding a Power BI Dashboard: A Secure and Simple Solution. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
