PowerBI Api 获取访问令牌

问题描述 投票:0回答:1

我想要自动登录以在 mvc 应用程序中使用 iframe 查看 power bi 报告。

为此,我在 Azure AD 中创建了一个应用程序,并在委托权限中授予了所有读取权限。

为此,我创建了一个类并获取访问令牌。但是当我使用令牌调用服务时,我收到未经授权的错误。

我创建的类如下;

 public async Task<string> GetAccessTokenAsync()
 {
     IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(clientId)
         .WithClientSecret(clientSecret)
         .WithAuthority(new Uri($"https://login.microsoftonline.com/{tenantId}"))
         .Build();


     string[] scopes = new string[] { "https://analysis.windows.net/powerbi/api/.default" };
     AuthenticationResult result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
     Console.WriteLine("token: " + result.AccessToken);
     return result.AccessToken;
 }


 public async Task<string> GetReportsAsync()
 {
     string accessToken = await GetAccessTokenAsync();

     using HttpClient client = new HttpClient();
     client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

     HttpResponseMessage response = await client.GetAsync("https://api.powerbi.com/v1.0/myorg/reports");
     return await response.Content.ReadAsStringAsync();
 }

如何解决这个问题?

oauth-2.0 powerbi power-bi-report-server powerbi-api
1个回答
0
投票

此 API 调用从 我的工作区获取报告:

https://api.powerbi.com/v1.0/myorg/reports

但是由于应用程序注册没有个人工作区,因此返回错误。


我建议改用这个 api 调用

https://api.powerbi.com/v1.0/myorg/groups/{groupId}/reports/{reportId}

此调用返回一个嵌入 URL,可用于将 Power BI 报表直接嵌入到您的应用程序中。

© www.soinside.com 2019 - 2024. All rights reserved.