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

我有一些使用Power BI App的客户端 .
使用此API,我希望将所有报告( .pbix )添加到我的文件夹中 .

我怎么能得到它们?

回答(1)

2 years ago

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

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

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

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
}