使用服务主体对 GetReportInGroupAsync PowerBI Embedded API 调用进行未经授权的响应

Hay*_*ash 3 .net c# powerbi asp.net-core powerbi-embedded

我正在尝试将 Power BI 报告嵌入到我的 .Net Core 应用程序中,但是我无法从请求中获得有效的响应。我正在使用 Microsoft.PowerBI.API 包和一个向服务主体注册的 azure 应用程序。

据我所知,我已经按照以下说明设置了 AAD 和服务主体,并使用了正确的权限:https : //docs.microsoft.com/en-us/power-bi/developer/embedded/embed-服务负责人

我发现在将该安全组添加到 Power BI 管理门户中的管理设置之前,您必须将 AAD/服务主体添加到安全组的部分。

这是我正在使用的代码片段:

var AuthorityUrl = "https://login.microsoftonline.com/common/";
var ResourceUrl = "https://analysis.windows.net/powerbi/api";

var authenticationContext = new AuthenticationContext(AuthorityUrl);
AuthenticationResult authenticationResult = null;
var credential = new ClientCredential("application_id", "application_secret");
authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, credential);

using (var client = new Microsoft.PowerBI.Api.PowerBIClient(new Uri("https://api.powerbi.com/"), new TokenCredentials(authenticationResult.AccessToken, "Bearer")))
{
    var report = await client.Reports.GetReportInGroupAsync(new Guid("workspace_id"), new Guid("report_id"));
}
Run Code Online (Sandbox Code Playgroud)

我成功地从AcquireTokenAsync通话中取回了令牌,但是在收到报告时,我得到了一个未经授权的令牌。

以下是我在 Azure 中的 AAD/服务主体的权限: azure aad api 权限

这是我用 Fiddler 捕获的请求/响应。要求:

GET https://api.powerbi.com/v1.0/myorg/groups/{workspace_id}/reports/{report_id} HTTP/1.1
Authorization: Bearer {access_token}
User-Agent: FxVersion/4.6.28207.03 OSName/Windows OSVersion/Microsoft.Windows.10.0.18362. Microsoft.PowerBI.Api.PowerBIClient/3.14.01300.0002
Host: api.powerbi.com
Run Code Online (Sandbox Code Playgroud)

回复:

HTTP/1.1 401 Unauthorized
Content-Length: 0
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Frame-Options: deny
X-Content-Type-Options: nosniff
Access-Control-Expose-Headers: RequestId
request-redirected: true
home-cluster-uri: https://wabi-west-us-redirect.analysis.windows.net/
RequestId: {request_id}
Date: Thu, 10 Sep 2020 16:23:07 GMT
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激!

Ana*_*mar 5

要使服务主体适用于任何嵌入,您需要在 Power BI 管理门户中启用服务主体选项,然后将其添加到 Power BI 工作区。

请检查以下内容一次:

  1. 检查是否在 Power BI 服务的管理门户下启用了服务主体。从第 3 步开始
  2. 如果在执行上述步骤后仍然不适合您,请尝试嵌入开发人员示例

此外,根据您的代码,您似乎正在使用 ADAL 库进行身份验证。Microsoft建议使用 MSAL 库对 Azure AD 实体进行身份验证。
此外,您可以使用证书而不是应用程序机密进行服务主体身份验证。(文档)