Forum Discussion

mariack's avatar
mariack
New Member
1 year ago
Solved

Error when embedding Power BI report in app: Operation returned an invalid status code 'Forbidden'

I am embedding a Power BI report in a Blazor web app. It was working fine until today, when it suddenly stopped working. Now, navigating to a page in the app which contains an embedded Power BI report results in the following error:

Error: Microsoft.Rest.HttpOperationException: Operation returned an invalid status code 'Forbidden'

 

The app uses a service principal to authenticate with the Power BI workspace.

 

Power BI license info:

 
App code:
Below is the Blazor code that I'm using to generate the info for embedding the report in the app, and the error arises in the "GenerateTokenAsync()" line:

 

 

// Retrieve the necessary info to embed a Power BI report in the app
public async Task<EmbeddedReportViewModel> GetReportEmbedding(string workspaceId, string reportId)
{
var tenantId = _configuration["AzureAd:TenantId"];
var clientId = _configuration["AzureAd:ClientId"];
var clientSecret = _configuration["AzureAd:ClientSecret"];
var authorityUri = new Uri($"https://login.microsoftonline.com/{tenantId}");

var app = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(authorityUri)
.Build();

var powerbiApiDefaultScope = "https://analysis.windows.net/powerbi/api/.default";
var scopes = new string[] { powerbiApiDefaultScope };

var authResult = await app.AcquireTokenForClient(scopes).ExecuteAsync();

string accessToken = authResult.AccessToken;

// Create a Power BI client which will interact with Power BI's REST API
var tokenCredentials = new TokenCredentials(accessToken, "Bearer");
var urlPowerBiServiceApiRoot = "https://api.powerbi.com/";
var pbiClient = new PowerBIClient(new Uri(urlPowerBiServiceApiRoot), tokenCredentials);

// Fetch meta data for the desired Power BI report
var workspaceIdGuid = new Guid(workspaceId);
var reportIdGuid = new Guid(reportId);
var report = pbiClient.Reports.GetReportInGroup(workspaceIdGuid, reportIdGuid);

// Generate embed token which grants permission to view the report
var tokenRequest = new GenerateTokenRequest(TokenAccessLevel.View, report.DatasetId);
var embedTokenResponse = await pbiClient.Reports.GenerateTokenAsync(workspaceIdGuid, reportIdGuid, tokenRequest);

var reportViewModel = new EmbeddedReportViewModel(
report.Id.ToString(),
report.Name,
report.EmbedUrl,
embedTokenResponse.Token
);

return reportViewModel;
}
​

 

 

Any help is very appreciated! 

Thank you,
Maria
  • We ended up getting things to work by creating an entirely new service principal (Azure App Registration) and switching all of the relevant Power BI settings over to that new service principal. Not a very satisfying solution, but for some reason it did the trick.

3 Replies

  • We ended up getting things to work by creating an entirely new service principal (Azure App Registration) and switching all of the relevant Power BI settings over to that new service principal. Not a very satisfying solution, but for some reason it did the trick.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi mariack,
    Thanks for reaching out to the Microsoft fabric community forum.

    The embedding feature might be disabled in the Power BI admin settings, preventing content from being embedded successfully. To resolve this, navigate to the Power BI Admin portal, go to Tenant settings, and ensure that the Embed content in apps setting is enabled. This will allow embedding to function properly within applications.
     
    The application or service principal may not have adequate permissions to access the desired resource, leading to insufficient permission errors. To resolve this, ensure that the application has been granted the appropriate permissions. For example, when working with Power BI Embedded, verify that the service principal has the necessary rights to generate access tokens. Properly configuring permissions will help prevent authorization issues and ensure seamless access.

    If you find this post helpful, please mark it as an "Accept as Solution" and consider giving a KUDOS.
    Thanks and Regards

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi mariack,

    May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

    Thank you.