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);
我使用了dot net core和MVC 出现此错误我尝试为客户嵌入 power bi 但出现了同样的错误,过去两天我一直在尝试解决此错误,但无法解决
即使它不起作用,我也遇到了同样的错误
有什么我需要启用的东西吗?有没有我遗漏的步骤
我什至尝试嵌入微软给出的示例代码微软示例代码
注意到世界我不知道该怎么办
操作返回无效状态代码“未经授权” - Power bi 嵌入
如果您错过了在 Power Bi 工作区下授予对服务主体的访问权限,通常会发生此错误。
当我在工作区下运行类似的代码而不添加服务主体时,我也遇到了相同的错误操作返回了无效的状态代码“未经授权”:
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);
回复:
要解决错误,请确保在 Power Bi 工作区下添加具有正确访问权限的服务主体,如下所示:
几分钟后再次运行代码时,我成功获得了响应以及报告详细信息:
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);
回复: