我手头的问题是,我正在尝试自动化从 Power BI 下载报告的过程,并在 Python 的帮助下将其发送给各种 WhatsApp 联系人。
这可能吗?
我找到了可用于下载报告的 Microsoft REST API,但我在尝试配置我的凭据和其他内容时迷失了方向。
我手头的问题是,我正在尝试自动化从 Power BI 下载报告的过程,并在 Python 的帮助下将其发送给各种 WhatsApp 联系人。
这可能吗?
我找到了可用于下载报告的 Microsoft REST API,但我在尝试配置我的凭据和其他内容时迷失了方向。
查看以下案例Power BI API - How can I get reports from app.powerbi.com 中的回复?
如果您想使用 API 执行此操作,则需要在组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
}
此外,还有其他有用的链接: