Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

power bi embed for angular js web app using service pricncipal

We are trying to embed power bi into we app which is based on Angular JS using the Service principal (not master a/c)

we could able to get the access token but we are facing an error in getting the embed token for it.

 

Here, the power bi is connected to azure alalysis services (without any roles)

 

Even, using postman we could only able to get access token but not the embed token (may be we are missing some parameter to pass into the API but not sure which one)

 

below is the js code which we have tried.

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);
    });
   }
   };

below is the error msg:

  1. code: "InvalidRequest"
  2. message: "Dataset provided for effective identity is different from element's dataset 5fa25d64-8086-4bcb-afbb-4a43fb2e03e3"

 

0 REPLIES 0

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.