1
votes

I am trying to embed a power bi report into webpage using the sample provided by this link.

In this sample, I have replaced all the client ids and username password of my pro account.

I have set the Native app type in App Registration. When I am trying to get Embed Token, following error is coming: enter image description here

I have given following permissions to my app: enter image description here enter image description here

Here is my code:

public async Task<ActionResult> EmbedReport()
        {

            // Create a user password credentials.
            UserPasswordCredential UserCredentials = new UserPasswordCredential(Username, Password);
            AuthenticationContext AuthContext = new AuthenticationContext(AuthorityUrl, false);

            // Authenticate using created credentials
            //AuthenticationResult AuthResult = await AuthContext.AcquireTokenAsync(ResourceUrl, clientCred);

            AuthenticationResult AuthResult = await AuthContext.AcquireTokenAsync(ResourceUrl, ClientId, UserCredentials);

            if (AuthResult == null)
            {
                return View(new EmbedConfig()
                {
                    ErrorMessage = "Authentication Failed."
                });
            }

            var tokenCredentials = new TokenCredentials(AuthResult.AccessToken, "Bearer");

            // 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 = client.Reports.GetReports();

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

                if (report == null)
                {
                    return View(new EmbedConfig()
                    {
                        ErrorMessage = "Group has no reports."
                    });
                }
                string accessLevel = "View";
                // Generate Embed Token.
                var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel, report.DatasetId,false);
                var tokenResponse = client.Reports.GenerateToken(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
                };

                return View(embedConfig);
            }
        }
1

1 Answers

1
votes

I think you need to add the permission "Access the directory as the signed-in user".
I will show you how I integrate reports and dashboards for my application for users who don't have Power BI account.

1 - First I have registered the application as Native app.
2 - In the Azure Portal, in Azure Active Directory, I grant permissions for the application: enter image description here

3 - Then, set the permissions of the application like this:

enter image description here

enter image description here

And the code I use the same like you.

Hope it helps.

Regards.