0

大家好。我可能没有完全理解如何在我的 Xamarin 应用程序中嵌入 PBI 报告。我得到了我的访问令牌并为此使用了主用户,但是在我 GET EmbedUrl 并将其放入 WebView 的 Source 属性之后,我收到一条消息:“请唱歌以查看此报告”。这是一个问题,因为我的客户没有 PowerBi 帐户,我希望他们能够看到此报告。问题可能出在我用来获取访问令牌的标题中,所以下面是我使用的代码

string url = "https://login.microsoftonline.com/" + tennant_id + "/oauth2/token"; //url to which I POST to

IEnumerable<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("authenticationType", "MasterUser"),
new KeyValuePair<string, string>("name", "<name>"),
new KeyValuePair<string, string>("workspaceId", "<workspaceId>"),
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("scope", "https://analysis.windows.net/powerbi/api/.default"),
new KeyValuePair<string, string>("resource", "https://analysis.windows.net/powerbi/api"),
new KeyValuePair<string, string>("authorityUrl", "https://login.microsoftonline.com/organizations/"),
new KeyValuePair<string, string>("urlPowerBiServiceApiRoot", "https://api.powerbi.com/"),
new KeyValuePair<string, string>("client_id", "<clientID>"),
new KeyValuePair<string, string>("username", "<username>"),
new KeyValuePair<string, string>("password", "<password>"),
new KeyValuePair<string, string>("client_secret", "<clientSecret>")
};

 获得访问令牌后,我在 GET 请求中使用它来 

https://api.powerbi.com/v1.0/myorg/reports/<report_it>

 我通过 AuthenticationHeader 作为 

client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);

然后我得到了我在 WebView Source 属性中使用的嵌入 URL,但我没有登录。当我登录时,我看到了这个报告,否则我不能。我在哪里犯了错误/错误?我是 PowerBi 嵌入的新手,所以请像对孩子一样解释它。

4

1 回答 1

1

我做到了,我会在这里为将来遇到类似问题的任何人发布解决方案。正如@AndreyNikolov 所说,我必须使用 JavaScript SDK,但他的参考资料有点陈旧,那里的东西不再完全有用,但它们起到了很好的参考作用。无论如何,让我们从头开始,我必须将 WebView 放在 xaml 文件中,如下所示:

<WebView x:Name="WVtest" HeightRequest="300" WidthRequest="50" Navigated="WVtest_Navigated">
            
</WebView>

然后我从JavaScript API添加了文件,但我无法让它们工作(因为它们让那里的文档变得非常复杂)所以我做了一些更改并使用了你可以在我的github repo上找到的文件。这些文件用于从嵌入 url 显示您的 PBI Embedded 报告,因此它们是必不可少的。我将它们放在 Android Assets 文件夹中,如下所示:

在此处输入图像描述

然后在构造函数后面的代码中,我添加了以下代码:

WVtest.Source = "file:///android_asset/html/index.html";

这样 WebView 就知道将其源设置为 index.html 文件。它将加载此 .html 并从中加载 .js 文件以调用 pbi 客户端并显示 PBI 报告。

“导航”事件是我将所有 REST API 调用到 PowerBI 和 Azure 的地方。首先,我发送 POST 请求以获取我的访问令牌。我发送 POST 请求的 URL 如下所示:

string url = "https://login.microsoftonline.com/" + tennant + "/oauth2/token";

其中tennant 是您的应用程序租户。对于身体,我需要几个参数,例如:

IEnumerable<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>()
{
    new KeyValuePair<string, string>("authentication_type",  "MasterUser"),
                        new KeyValuePair<string, string>("name",                "<name_of_app>"),
                        new KeyValuePair<string, string>("workspaceId",         "<workspace_id>"),
                        new KeyValuePair<string, string>("grant_type",          "password"),
                        new KeyValuePair<string, string>("scope",               "https://analysis.windows.net/powerbi/api/.default"),
                        new KeyValuePair<string, string>("resource",            "https://analysis.windows.net/powerbi/api"),
                        new KeyValuePair<string, string>("authorityUrl",        "<your auth url from azure, redirect url>"),
                        new KeyValuePair<string, string>("client_id",           "<client_id>"),
                        new KeyValuePair<string, string>("username",            "<username>"),
                        new KeyValuePair<string, string>("password",            "<pass>"),
                        new KeyValuePair<string, string>("client_secret",       "<clinet_secret>")
}

在我制作了这个键值对之后,我需要将它们转换为 POST 请求可以理解的东西。我在下面的代码中使用了 HttpContent:

HttpContent content = new FormUrlEncodedContent(parameters);
using (HttpResponseMessage response = await client.PostAsync(url, content)){
//rest of code from below
}

我将它们用作 POST 请求中的选项以从上面发送到 url。作为对该请求的响应,我得到了 access_token,这是我下一步需要的唯一东西。现在我向 powerBI API 发出 GET 请求:

string pbiUrl = "https://api.powerbi.com/v1.0/myorg/reports/<report_id>";
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken); //client is of type HttpClient

现在我得到了我的 embedUrl 作为响应,我需要像这样反序列化:

var response = await client.GetStringAsync(pbiUrl);

var rese = JsonConvert.DeserializeObject<PBIReport>(response2);
                                
await WVtest?.EvaluateJavaScriptAsync($"LoadEmbeddedObject('{rese.embedUrl}', '{accessToken}', '0', '{<reportId>}')"); //Here I call JavaScript function with parameters

第一个参数是包含来自响应的链接的 embedUrl,第二个是访问令牌,第三个是“0”,因为在 ReportLoader.js 中将零转换为 AAD 类型的令牌,如果您使用嵌入令牌,请在此处使用“1”。最后一个参数是您的 report_id。考虑到我在获取访问令牌时使用了它,我不知道为什么我必须再次使用它,但它需要在那里。

现在它应该可以正常工作了。给它一点时间加载,它真的很慢。如果您不喜欢报告的外观,请使用 ReportLoadder.js 中的配置对象,您在PowerBi JavaScript 客户端 wiki 上提供了很好的文档. 如果您有任何更简单的解决方案,请在此处发布,这只是对我有用的一种方法。随便问什么,我很乐意提供帮助。

于 2021-05-19T09:32:28.150 回答