You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure认证Power BI Embedded时触发Unauthorized错误求助

Power BI Embedded 公众访问认证触发Unauthorized错误排查求助

我正在尝试通过Azure认证实现Power BI Embedded报表的公众访问,但过程中触发了Unauthorized错误,具体情况如下:

  • 操作初期弹出权限确认弹窗,确认权限后出现错误(错误截图:错误截图)
  • 已确认appsettings.json中配置的凭据正确
  • Startup.cs中已添加如下认证配置代码:
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
            .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"), "AzureAd")
            .EnableTokenAcquisitionToCallDownstreamApi(Configuration.GetValue<string>("DownstreamApi:Scopes")?.Split(' '))
            .AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"))
            .AddInMemoryTokenCaches();
        
        services.AddScoped(typeof(PowerBiServiceApi));

        services.AddRazorPages()
            .AddMicrosoftIdentityUI();
  • 错误发生在PowerBiServiceApi获取访问令牌及调用Power BI API的过程中,该类代码如下:
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Identity.Web;
using Microsoft.PowerBI.Api;
using Microsoft.PowerBI.Api.Models;
using Microsoft.Rest;

namespace NDTSM2.Services.Implementations.PowerBI
{
    // 用于传递嵌入单个报表所需数据的视图模型类
    public class EmbeddedReportViewModel
    {
        public string Id;
        public string Name;
        public string EmbedUrl;
        public string Token;
    }


        public class PowerBiServiceApi
        {
            private ITokenAcquisition tokenAcquisition { get; }
            private string urlPowerBiServiceApiRoot { get; }
    
            public PowerBiServiceApi(IConfiguration configuration, ITokenAcquisition tokenAcquisition)
            {
                this.urlPowerBiServiceApiRoot = configuration["PowerBi:ServiceRootUrl"];
                this.tokenAcquisition = tokenAcquisition;
            }
    
            public const string powerbiApiDefaultScope = "https://analysis.windows.net/powerbi/api/.default";
    
            // 获取Azure AD令牌(即访问令牌)的方法
            public string GetAccessToken()
            {
                return this.tokenAcquisition.GetAccessTokenForAppAsync(powerbiApiDefaultScope).Result;
            }
    
            public PowerBIClient GetPowerBiClient()
            {
                var tokenCredentials = new TokenCredentials(GetAccessToken(), "Bearer");
                return new PowerBIClient(new Uri(urlPowerBiServiceApiRoot), tokenCredentials);
            }
            public async Task<EmbeddedReportViewModel> GetReport(Guid WorkspaceId, Guid ReportId)
            {
    
                PowerBIClient pbiClient = GetPowerBiClient();
    
                // 调用Power BI服务API获取嵌入数据
                var report = await pbiClient.Reports.GetReportInGroupAsync(WorkspaceId, ReportId);
    
                // 为报表生成只读嵌入令牌
                var datasetId = report.DatasetId;
                var tokenRequest = new GenerateTokenRequest(TokenAccessLevel.View, datasetId);
                var embedTokenResponse = await pbiClient.Reports.GenerateTokenAsync(WorkspaceId, ReportId, tokenRequest);
                var embedToken = embedTokenResponse.Token;
    
                // 将报表嵌入数据返回给调用方
                return new EmbeddedReportViewModel
                {
                    Id = report.Id.ToString(),
                    EmbedUrl = report.EmbedUrl,
                    Name = report.Name,
                    Token = embedToken
                };
    
            }
        }
    
    }
  • 后续补充的错误详情:

Operation returned an invalid status code 'Unauthorized'

at Microsoft.PowerBI.Api.ReportsOperations.GetReportInGroupWithHttpMessagesAsync(Guid groupId, Guid reportId, Dictionary`2 customHeaders, CancellationToken cancellationToken)
at Microsoft.PowerBI.Api.ReportsOperationsExtensions.GetReportInGroupAsync(IReportsOperations operations, Guid groupId, Guid reportId, CancellationToken cancellationToken)
at Microsoft.PowerBI.Api.ReportsOperationsExtensions.GetReportInGroup(IReportsOperations operations, Guid groupId, Guid reportId)
at NDTSM2.Services.Implementations.PowerBI.PbiEmbedService.GetEmbedParams(Guid workspaceId, Guid reportId, Guid additionalDatasetId) in C:\Users\cryof\Desktop\NDTMS4\Service\NDTSM2.SERVICES\Implementations\PowerBI\PbiEmbedService.cs:line 41
at NDTMS2.Web.Controllers.EmbedInfoController.GetEmbedInfo() in C:\Users\cryof\Desktop\NDTMS4\NDTMS2.WEB\Controllers\EmbedInfoController.cs:line 40

错误触发于PbiEmbedService中的如下代码行:

var pbiReport = pbiClient.Reports.GetReportInGroup(workspaceId, reportId);

我已查阅相关资料但未解决问题,恳请各位提供排查该Unauthorized错误的方案,非常感谢!


内容的提问来源于stack exchange,提问作者CryoFusion87

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 16:10:37