Operation returned an invalid status code 'Unauthorized' - Power bi embeded

215 views Asked by At

Error Details: Operation returned an invalid status code 'Unauthorized' Microsoft.PowerBI.Api.ReportsOperations.GetReportInGroupWithHttpMessagesAsync(Guid groupId, Guid reportId, Dictionary\<string, List\<string\>\> customHeaders, CancellationToken cancellationToken) Microsoft.PowerBI.Api.ReportsOperationsExtensions.GetReportInGroupAsync(IReportsOperations operations, Guid groupId, Guid reportId, CancellationToken cancellationToken) testProjec.Services.PowerBiServiceApi.GetReport(Guid WorkspaceId, Guid ReportId) in PowerBiServiceApi.cs \+ var report = await pbiClient.Reports.GetReportInGroupAsync(WorkspaceId, ReportId); testProjec.Controllers.HomeController.Embed() in HomeController.cs \+ var viewModel = await powerBiServiceApi.GetReport(workspaceId, reportId);

I used dot net core and MVC this error comes up I tried to embed power bi for customer but the same error comes up i been trying to resolve this error for the last two days but i can't be solved

microsoft powerbi dotnet manual

microsoft powerbi html embed manual I

I tried seveal methods to resolve the problem like enabling the power bi apis in powerbi settings giving permision in azure portal

eventhougt it don't work I'm getting the same error

is there any thingh i need to enable is there any steps I'm missing

I even tried embedding with the microsoft given sample code microsoft example code

noting world I don't know what to do

1

There are 1 answers

0
Imran On

Operation returned an invalid status code 'Unauthorized' - Power bi embeded

The error usually occurs if you missed granting access to service principal under your Power Bi workspace.

When I ran similar code without adding service principal under workspace, I too got same error Operation returned an invalid status code ‘Unauthorized’:

var credentials = new TokenCredentials(accessToken, "Bearer");
var client = new PowerBIClient(new Uri(apiUrl), credentials);

var groupIdGuid = Guid.Parse("groupId");
var reportIdGuid = Guid.Parse("reportId");

var report = client.Reports.GetReportInGroup(groupIdGuid, reportIdGuid);

Console.WriteLine();
Console.WriteLine("Report ID: " + report.Id);
Console.WriteLine("Report Name: " + report.Name);
Console.WriteLine("Report Web URL: " + report.WebUrl);

Response:

enter image description here

To resolve the error, make sure to add the service principal under your Power Bi workspace with proper access like below:

enter image description here

When I ran the code again after few minutes, I got the response successfully with report details:

using Microsoft.Identity.Client;
using Microsoft.PowerBI.Api;
using Microsoft.Rest;

var clientId = "appId";
var clientSecret = "secret";
var scope = "https://analysis.windows.net/powerbi/api/.default";

var apiUrl = "https://api.powerbi.com/";
var groupId = "groupId";
var reportId = "reportId";

var app = ConfidentialClientApplicationBuilder.Create(clientId)
    .WithClientSecret(clientSecret)
    .WithAuthority(new Uri("https://login.microsoftonline.com/tenantId"))
    .Build();
var result = app.AcquireTokenForClient(new string[] { scope }).ExecuteAsync().Result;
var accessToken = result.AccessToken;

var credentials = new TokenCredentials(accessToken, "Bearer");
var client = new PowerBIClient(new Uri(apiUrl), credentials);

var groupIdGuid = Guid.Parse(groupId);
var reportIdGuid = Guid.Parse(reportId);

var report = client.Reports.GetReportInGroup(groupIdGuid, reportIdGuid);

Console.WriteLine();
Console.WriteLine("Report ID: " + report.Id);
Console.WriteLine("Report Name: " + report.Name);
Console.WriteLine("Report Web URL: " + report.WebUrl);

Response:

enter image description here

Reference:
Embed content in your Power BI embedded analytics application | Microsoft