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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
rahulpower
New Member

Power BI Embed report JavaScript

How to Embed Power BI Reports for my Own Application so that non-bi users can access it. Apart from using by Iframe i want to use Rest API. 

I am Stuck how to generate this token and how to get this.  

I have Referred this Project  https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html to testing. 

 

https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html

 

In This, i want to know how can I get from my Power BI Report. I have created some sample Reports and want it to show for my own application for non- BI user 

1. Embed Token

2. Embed URL

3. report id 

 

I have already registered by application in https://dev.powerbi.com/apps

made an account in Azure Portal but can't get 

 

Please Provide Step by Step with images if can be 

 issue.png

1 ACCEPTED SOLUTION

Hi,

You can find all the steps needed to setup the enviroment and start embedding,
in this link- https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-embedding/

View solution in original post

6 REPLIES 6
NavasMS
Employee
Employee

How to apply trend line on existing report embed using power bi client api or authoring client api? is there js client methods available to do that? please provide pointers

Eric_Zhang
Employee
Employee

@rahulpower

The process for embedding for non power bi users are all in this demo App Owns Data.

To get the Embedded Token, Embed URL and report id, the first step is to get an accesstoken, see code snippet from this HomeController.cs.

 

 

            var credential = new UserPasswordCredential(Username, Password);

            // Authenticate using created credentials
            var authenticationContext = new AuthenticationContext(AuthorityUrl);
            var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);

 

 

And then

 

// Create a Power BI Client object. It will be used to call Power BI APIs.
            using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
            {
                 
                // Get a list of reports.
                var reports = await client.Reports.GetReportsInGroupAsync(GroupId);

                // Get the first report in the group.
                var report = reports.Value.FirstOrDefault();

                if (report == null)
                {
                    return View(new EmbedConfig()
                    {
                        ErrorMessage = "Group has no reports."
                    });
                }

                // Generate Embed Token.
                var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
                var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, report.Id, generateTokenRequestParameters);

                if (tokenResponse == null)
                {
                    return View(new EmbedConfig()
                    {
                        ErrorMessage = "Failed to generate embed token."
                    });
                }

                // Generate Embed Configuration.
                var embedConfig = new EmbedConfig()
                {
                    EmbedToken = tokenResponse,
                    EmbedUrl = report.EmbedUrl,
                    Id = report.Id
                };

Actually, underlying the code calls those POWER BI REST APIs Get Reports and GenerateToken .

 

@Eric_Zhang I have been folowing these article 

 

https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-get-azuread-access-token/

Access token for non-Power BI users (app owns data)

 

and have patterned my web app's Controller.cs to get an Embed Token.. unfortunately, I am stuck with an exception that I am not sure what the root cause is.

 

What has been so far:

 

1. An AAD user with Power BI Pro License

2. App workspace with 1 dashboard uploaded - connected to our CUBE hosted in Azure Analysis Services server

3. Registered my web-app into Azure AAD, assigned permissions into both Power BI service and AAD

 

I am able to login to AAD using the ClientID and ClientSecret:

 

 

var clientId = ""; // took out actual value
var clientSecret = ""; // took out actual value
var authContext = new AuthenticationContext("https://login.windows.net/common/oauth2/authorize/");
var credential = new ClientCredential(clientId, clientSecret);
var authRes = (AuthenticationResult)authContext.AcquireTokenAsync("https://analysis.windows.net/powerbi/api", credential).Result;

var aadToken = authRes.AccessToken; // all good!

// get Embed Token for app workspace dashboard identified by groupId and dashboardId
var groupId = ""; // took out actual value
var dashboardId = ""; // took out actual value

var tokenCredentials = new TokenCredentials(aadToken , "Bearer");
using (var client = new PowerBIClient(new Uri("https://api.powerbi.com/"), tokenCredentials))
{
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");

// GenerateTokenInGroup is throwing exception: Operation returned an invalid status code 'Forbidden'
var tokenResponse = client.Dashboards.GenerateTokenInGroup(groupId, dashboardId, generateTokenRequestParameters); // Generate Embed Configuration. var embedConfig = new { EmbedToken = tokenResponse, EmbedUrl = "", Id = dashboardId }; }

 

First difference I can note of between the sample and my implementation is I am not using the Async version of the method. I am using GenerateTokenInGroup instead of GenerateTokenInGroupAsync. Is it required to use GenerateTokenInGroupAsync ?

 

I haven't setup anything in Power BI Premium or that one where we need to assign capacity, I don't think that's required in development stage. 

 

What am I missing?

 

 

Thanks a lot. Need to Be Done in Javascript can you Give me the steps or help out how to create azure and all the thinks by step by step 

thanks once again 

Hi,

You can find all the steps needed to setup the enviroment and start embedding,
in this link- https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-embedding/

Would a PowerBI Pro Trial version work?

Office 365 displays my Power BI account as Free.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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