"This content is not available" 在 App Owns Data 场景中创建报告时

"This content is not available" when creating report in App Owns Data scenario

我正在使用 ASP.NET Core 3.1 MVC 应用程序,我试图在嵌入式视图中使用“应用程序拥有数据”方案并使用服务主体来实现 Power BI。

我可以使用上述服务主体成功连接到 Power BI 服务。我能够毫无问题地检索令牌并将它们传递给 Power BI 服务,在那里我可以毫无问题地检索报告列表和查看单个报告(使用 powerbi.embed())。但是,当我调用 powerbi.createReport() JavaScript 方法时,在 Fiddler 中查看时,它总是 returns“此内容不可用”以及 403 响应。

生成访问令牌

    private async Task<string> GetPowerBIAccessToken()
    {
        string pBiClientId = powerBISettings.ApplicationId.ToString();
        string pBiSecret = powerBISettings.ApplicationSecret.ToString();

        var clientCredentials = new ClientCredential(pBiClientId, pBiSecret);
        var authContext = new AuthenticationContext($"{powerBISettings.AuthorityUrl}/{powerBISettings.TenantId}", new TokenCache());
        var token = await authContext.AcquireTokenAsync(powerBISettings.ResourceUrl, clientCredentials);
        return token.AccessToken;        
    }

生成模型以查看 Power BI 报告的代码

    public async Task<EmbedConfig> ViewReport(Guid ReportId)
    {
        var result = new EmbedConfig {  };
        var accessToken = await GetPowerBIAccessToken();
        var tokenCredentials = new TokenCredentials(accessToken, "Bearer");

        using (var client = new PowerBIClient(new Uri(powerBISettings.ApiUrl), tokenCredentials))
        {
            var workspaceId = powerBISettings.WorkspaceId.Value;
            var reports = await client.Reports.GetReportsInGroupAsync(workspaceId);
            var report = reports.Value.Where(a => a.Id == ReportId).FirstOrDefault();

            var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
            var tokenResponse = await client.Reports.GenerateTokenAsync(workspaceId, report.Id, generateTokenRequestParameters);

            result.EmbedToken = tokenResponse;
            result.EmbedUrl = report.EmbedUrl;
            result.Id = report.Id == null ? string.Empty : report.Id.ToString();
        }
        return result;
    }

带有嵌入式报告的 MVC 视图以查看以上报告 - 这有效!

<style>
#reportContainer {
    height: 600px;
    width: 100%;
    max-width: 2000px;
}
</style>
<script src="https://npmcdn.com/es6-promise@3.2.1"></script>
<script src="~/js/powerbi.min.js"></script>

<div id="reportContainer"></div>
@section scripts {
<script>
// Read embed application token from Model
var accessToken = "@Model.EmbedToken.Token";

// Read embed URL from Model
var embedUrl = "@Html.Raw(Model.EmbedUrl)";

// Read report Id from Model
var embedReportId = "@Model.Id";

// Get models. models contains enums that can be used.
var models = window['powerbi-client'].models;

// Embed configuration used to describe the what and how to embed.
// This object is used when calling powerbi.embed.
// This also includes settings and options such as filters.
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
var config = {
    type: 'report',
    tokenType: models.TokenType.Embed,
    accessToken: accessToken,
    embedUrl: embedUrl,
    id: embedReportId,
    permissions: models.Permissions.All,
    settings: {
        filterPaneEnabled: true,
        navContentPaneEnabled: true
    }
};

$(document).ready(function () {
    // Get a reference to the embedded report HTML element
    var reportContainer = $('#reportContainer')[0];

    // Embed the report and display it within the div container.
    powerbi.embed(reportContainer, config);
});
</script>
}

生成模型以创建新空白报告的代码

public async Task<EmbedConfig> CreateNewReport()
    {
        var result = new EmbedConfig { };
        var accessToken = await GetPowerBIAccessToken();
        var tokenCredentials = new TokenCredentials(accessToken, "Bearer");

        using (var client = new PowerBIClient(new Uri(powerBISettings.ApiUrl), tokenCredentials))
        {
            var workspaceId = powerBISettings.WorkspaceId.Value;
            var generateTokenRequestParameters = new GenerateTokenRequest(TokenAccessLevel.Create, datasetId: "xyz", allowSaveAs: true);
            var tokenResponse = await client.Reports.GenerateTokenForCreateInGroupAsync(workspaceId, generateTokenRequestParameters);

            result.EmbedToken = tokenResponse;
            result.DatasetId = "xyz";
        }

        return result;
    }

带嵌入式创建报告的 MVC 视图 - 不起作用

<style>
#reportContainer {
    height: 600px;
    width: 100%;
    max-width: 2000px;
}
</style>
<script src="https://npmcdn.com/es6-promise@3.2.1"></script>
<script src="~/js/powerbi.min.js"></script>

<div id="reportContainer"></div>

@section scripts {
<script>
// Read embed application token from Model
var accessToken = '@Model.EmbedToken.Token';

// Get models. models contains enums that can be used.
var models = window['powerbi-client'].models;

// Embed configuration used to describe the what and how to embed.
// This object is used when calling powerbi.embed.
// This also includes settings and options such as filters.
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
    var config = {
    accessToken: accessToken,
    datasetId: '@Model.DatasetId',
    embedUrl: 'https://embedded.powerbi.com/ReportEmbed'
};

$(document).ready(function () {
    // Get a reference to the embedded report HTML element
    var reportContainer = $('#reportContainer')[0];

    // Embed the report and display it within the div container.
    powerbi.createReport(reportContainer, config);
});
</script>
}

加载上面的视图时 returns this

Fiddler 捕获 403 响应

有人知道为什么会这样吗?服务主体拥有分配给 Power BI 服务的所有 API 权限(我按照 Power BI 中的 'Create App' 向导在 Azure AD 中创建应用程序注册)。我只是不明白为什么它一直返回 403 - 任何帮助将不胜感激!

在您链接的文档中提到了以下内容:

When embedding, the minimum requirements are to have a type, embedUrl, accessToken and id. 

在您不起作用的示例中,您似乎没有指定 tokenType。只需 embedUrl、accessToken 和 Id。