Power BI API-如何从app.powerbi.com获取报告?

时间:2018-12-10 10:09:10

标签: powerbi powerbi-datasource

我有一些使用Power BI App的客户端。
使用此API,我想将所有报告(.pbix)都保存到我的文件夹中。

我如何获得它们?

1 个答案:

答案 0 :(得分:1)

您只需在浏览器中打开报告并将其另存为.pbix文件即可:

enter image description here

如果要使用API​​进行此操作,则需要Export Report In Group REST API。要使用它,您需要获取访问令牌并将其添加到您的请求标头中。您可以通过调用AcuireToken中的某些ADAL方法来获取它。

您可以使用这样的代码(请注意,示例中没有错误检查):

string clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // Obtain at https://dev.powerbi.com/apps
string redirectUri = "https://login.live.com/oauth20_desktop.srf";
string resourceUri = "https://analysis.windows.net/powerbi/api";
string authorityUri = "https://login.windows.net/common/oauth2/authorize";

AuthenticationContext authContext = new AuthenticationContext(authorityUri, new TokenCache()); // PM> Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory

var authenticationResult = await authContext.AcquireTokenAsync(resourceUri, clientId, new Uri(redirectUri), new PlatformParameters(PromptBehavior.Auto));
var accessToken = authenticationResult.AccessToken);

string powerBIApiUrl = "https://api.powerbi.com/v1.0/myorg/groups/{groupId}/reports/{reportKey}/Export"; // Replace groupId and reportKey with actual values

var request = WebRequest.Create(powerBIApiUrl) as HttpWebRequest;
request.KeepAlive = true;
request.Method = "GET";
request.ContentLength = 0;
request.ContentType = "application/json";
request.Headers.Add("Authorization", $"Bearer {accessToken}");

using (HttpWebResponse httpResponse = request.GetResponse() as System.Net.HttpWebResponse)
{
    //Read httpResponse.GetResponseStream() to get the .pbix file
}