嵌入 Power BI 工作区集合中的报表Embed a report in Power BI Workspace Collections

了解如何将 Azure Power BI 工作区集合中的报表嵌入到应用程序中。Learn how to embed a report that is in Power BI Workspace Collections into your application.

Important

Power BI 工作区集合已弃用,到 2018 年 6 月 或合同指示时可用。Power BI Workspace Collections is deprecated and is available until June 2018 or when your contract indicates. 建议你规划到 Power BI Embedded 的迁移以避免应用程序中断。You are encouraged to plan your migration to Power BI Embedded to avoid interruption in your application. 有关如何将数据迁移到 Power BI Embedded 的信息,请参阅如何将 Power BI 工作区集合内容迁移到 Power BI EmbeddedFor information on how to migrate your data to Power BI Embedded, see How to migrate Power BI Workspace Collections content to Power BI Embedded.

我们会探讨如何真正在应用程序中嵌入报表。We will look at how to actually embed a report into your application. 本文假设工作区集合中的工作区内已有一个报表。This is assuming you already have a report that exists within a workspace in your workspace collection. 如果尚未完成该步骤,请参阅 Power BI 工作区集合入门If you haven't done that step yet, see Get started with Power BI Workspace Collections.

可以配合 Power BI 工作区集合使用 .NET (C#) 或 Node.js SDK 和 JavaScript,轻松构建应用程序。You can use the .NET (C#) or Node.js SDK, along with JavaScript, to easily build your application with Power BI Workspace Collections.

借助访问密钥使用 REST APIUsing the access keys to use REST APIs

若要调用 REST API,可以传递访问密钥(可从给定工作区集合的 Azure 门户中获取)。In order to call the REST API, you can pass the access key which you can get from the Azure portal for a given workspace collection. 有关详细信息,请参阅 Power BI 工作区集合入门For more information, see Get started with Power BI Workspace Collections.

获取报表 IDGet a report ID

每个访问令牌基于某个报表。Every access token is based on a report. 需要获取想要嵌入的报表的给定报表 ID。You will need to get the given report id for the report that you want to embed. 可以通过调用 获取报表 REST API 来实现此目的。This can be done based on calls to the Get Reports REST API. 此操作返回报表 ID 和嵌入 URL。This will return the report id and the embed url. 可以使用 Power BI .NET SDK 或通过直接调用 REST API 来实现此目的。This can be done using the Power BI .NET SDK or calling the REST API directly.

使用 Power BI .NET SDKUsing the Power BI .NET SDK

使用 .NET SDK 时,需要创建基于从 Azure 门户获取的访问密钥的令牌凭据。When using the .NET SDK, you need to create a token credential that is based on the access key you get from the Azure portal. 这就需要安装 Power BI API NuGet 包This requires that you install the Power BI API NuGet package.

安装 NuGet 包NuGet package install

Install-Package Microsoft.PowerBI.Api

C# 代码C# code

using Microsoft.PowerBI.Api.V1;
using Microsoft.Rest;

var credentials = new TokenCredentials("{access key}", "AppKey");
var client = new PowerBIClient(credentials);
client.BaseUri = new Uri(https://api.powerbi.cn);

var reports = (IList<Report>)client.Reports.GetReports(workspaceCollectionName, workspaceId).Value;

// Select the report that you want to work with from the collection of reports.

直接调用 REST APICalling the REST API directly

System.Net.WebRequest request = System.Net.WebRequest.Create("https://api.powerbi.cn/v1.0/collections/{collectionName}/workspaces/{workspaceId}/Reports") as System.Net.HttpWebRequest;

request.Method = "GET";
request.ContentLength = 0;
request.Headers.Add("Authorization", String.Format("AppKey {0}", accessToken.Value));

using (var response = request.GetResponse() as System.Net.HttpWebResponse)
{
    using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
    {
        // Work with JSON response to get the report you want to work with.
    }

}

创建访问令牌Create an access token

Power BI 工作区集合使用嵌入标记,即 HMAC 签名的 JSON Web 令牌。Power BI Workspace Collections use embed tokens, which are HMAC signed JSON Web Tokens. 令牌已使用 Power BI 工作区集合中的访问密钥签名。The tokens are signed with the access key from your Power BI Workspace Collection. 默认情况下,嵌入令牌用于提供对要嵌入到应用程序的报表的只读访问权限。Embed tokens, by default, are used to provide read-only access to a report to embed into an application. 嵌入令牌是针对特定报表颁发的,应该与嵌入 URL 相关联。Embed tokens are issued for a specific report and should be associated with an embed URL.

应在服务器上创建访问令牌,因为要使用访问密钥对令牌进行签名/加密。Access tokens should be created on the server as the access keys are used to sign/encrypt the tokens. 有关如何创建访问令牌的信息,请参阅通过 Power BI 工作区集合进行身份验证和授权For information on how to create an access token, see Authenticating and authorizing with Power BI Workspace Collections. 此外,还可以查看 CreateReportEmbedToken 方法。You can also review the CreateReportEmbedToken method. 以下示例演示了如何使用用于 Power BI 的 .NET SDK。Here is an example of what this would look like using the .NET SDK for Power BI.

将使用前面检索到的报表 ID。You use the report ID that you retrieved earlier. 创建嵌入令牌后,将使用访问密钥来生成可从 JavaScript 的角度使用的令牌。Once the embed token is created, you will then use the access key to generate the token that you can use from the javascript perspective. PowerBIToken 类要求安装 Power BI Core NuGut 包The PowerBIToken class requires that you install the Power BI Core NuGut Package.

安装 NuGet 包NuGet package install

Install-Package Microsoft.PowerBI.Core

C# 代码C# code

using Microsoft.PowerBI.Security;

// rlsUsername, roles and scopes are optional.
embedToken = PowerBIToken.CreateReportEmbedToken(workspaceCollectionName, workspaceId, reportId, rlsUsername, roles, scopes);

var token = embedToken.Generate("{access key}");

将权限范围添加到嵌入令牌Adding permission scopes to embed tokens

使用 Embed 令牌时,对于允许对其进行访问的资源,你可能希望限制其使用。When using Embed tokens, you may want to restrict usage of the resources you give access to. 为此,可以生成一个权限范围受到约束的令牌。For this reason, you can generate a token with scoped permissions. 有关详细信息,请参阅范围For more information, see Scopes

使用 JavaScript 嵌入Embed using JavaScript

获取访问令牌和报表 ID 后,可以使用 JavaScript 来嵌入报表。After you have the access token and the report ID, we can embed the report using JavaScript. 这就需要安装 NuGet Power BI JavaScript 包This requires that you install the NuGet Power BI JavaScript package. embedUrl 将只是 https://embedded.powerbi.cn/appTokenReportEmbed。The embedUrl will just be https://embedded.powerbi.cn/appTokenReportEmbed.

Note

可以使用 JavaScript 报表嵌入示例测试功能。You can use the JavaScript Report Embed Sample to test functionality. 我们还提供了适用于不同操作的代码示例。It also gives code examples for the different operations that are available.

安装 NuGet 包NuGet package install

Install-Package Microsoft.PowerBI.JavaScript

JavaScript 代码JavaScript code

<script src="/scripts/powerbi.js"></script>
<div id="reportContainer"></div>

<script>
var embedConfiguration = {
    type: 'report',
    accessToken: 'eyJ0eXAiO...Qron7qYpY9MI',
    id: '5dac7a4a-4452-46b3-99f6-a25915e0fe55',
    embedUrl: 'https://embedded.powerbi.cn/appTokenReportEmbed'
};

var $reportContainer = $('#reportContainer');
var report = powerbi.embed($reportContainer.get(0), embedConfiguration);
</script>

设置嵌入元素的大小Set the size of embedded elements

报表根据其容器大小自动嵌入。The report will automatically be embedded based on the size of its container. 若要重写默认的嵌入项大小,只需添加 CSS 类属性,或者宽度和高度的内联样式。To override the default size of the embedded item, simply add a CSS class attribute or inline styles for width and height.

另请参阅See also

示例入门Get started with sample
在 Power BI 工作区集合中进行身份验证和授权Authenticating and authorizing in Power BI Workspace Collections
CreateReportEmbedTokenCreateReportEmbedToken
JavaScript 嵌入示例JavaScript Embed Sample
Power BI JavaScript 包Power BI JavaScript package
Power BI API NuGet 包 Power BI Core NuGut 包Power BI API NuGet package Power BI Core NuGut Package
PowerBI-CSharp Git 存储库PowerBI-CSharp Git Repo
PowerBI-Node Git 存储库PowerBI-Node Git Repo

有更多问题?More questions? 试用 Power BI 社区Try the Power BI Community