-1

The problem statement I have in hand is, I am trying to automate the process of downloading reports from Power BI and send it to the various WhatsApp contacts with the help of Python.

Is this possible?

I found the Microsoft REST APIs which can be used to download the reports but I am getting lost in trying to configure my credentials and other things.

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97

1 Answers1

1

Check the reply in the following case Power BI API - How can I get reports from app.powerbi.com?

If you want to do this using an API, you will need Export Report In Group REST API. To use it, you need to acquire an access token and add it to your request header. You can acquire it by calling some of the AcuireToken methods from ADAL.

You can use code like this (please note there is no error checking in the example):

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
}   

Also, there are other useful links:

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39