我正在尝试将 Power BI 报告嵌入到我的 .Net Core 应用程序中,但是我无法从请求中获得有效的响应。我正在使用 Microsoft.PowerBI.API 包和通过服务主体注册的 azure 应用程序。
据我所知,我已按照此处的说明设置了 AAD 和服务主体,并具有正确的权限:https://learn.microsoft.com/en-us/power-bi/developer/embedded/embed-service-principal https://learn.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
打电话,但是当我收到报告时,我收到了未经授权的信息。
Here are the permissions of my AAD/Service Principal in Azure:
这是我用 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
任何帮助,将不胜感激!