【发布时间】: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 调用中取回了令牌,但是在获取报告时我得到了未经授权的结果。
这是我用 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