【问题标题】:Unauthorized response on GetReportInGroupAsync PowerBI Embedded API call using Service Principal使用服务主体对 GetReportInGroupAsync PowerBI Embedded API 调用做出未经授权的响应
【发布时间】:2022-05-18 23:22:11
【问题描述】:

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

据我所知,我已按照此处的说明设置了具有正确权限的 AAD 和服务主体: https://docs.microsoft.com/en-us/power-bi/developer/embedded/embed-service-principal

我发现您必须先将 AAD/服务主体添加到安全组,然后再将该安全组添加到 Power BI 管理门户中的管理员设置。

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

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"));
}

我成功地从AcquireTokenAsync 调用中取回了令牌,但是在获取报告时我得到了未经授权的结果。

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

这是我用 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

回复:

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

任何帮助将不胜感激!

【问题讨论】:

    标签: c# .net asp.net-core powerbi powerbi-embedded


    【解决方案1】:

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

    请检查以下内容:

    1. 检查是否在 Power BI 服务的管理门户下启用了服务主体。 继续关注step 3
    2. 如果执行上述步骤后仍然无法正常工作,请尝试与开发人员一起嵌入 sample

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

    【讨论】:

    • 我实际上在您回答之前就知道了这一点。我们已经正确连接了 PowerBI 端,问题在于我们的身份验证。我们切换到 MSAL 库并能够让它工作!感谢您的回复!
    【解决方案2】:

    我在这里尝试做同样的事情: PowerBI - getting EmbedToken - Unauthorized.
    我仍然无法让它工作。如果我正在登录我的应用程序,而不是特定用户,那么需要将哪个组或用户添加到 power bi 工作区?添加应用程序没有选项

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多