Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 电源Bi嵌入不工作,Can';t使用主用户登录_C#_Xamarin_Xamarin.forms_Powerbi_Powerbi Embedded - Fatal编程技术网

C# 电源Bi嵌入不工作,Can';t使用主用户登录

C# 电源Bi嵌入不工作,Can';t使用主用户登录,c#,xamarin,xamarin.forms,powerbi,powerbi-embedded,C#,Xamarin,Xamarin.forms,Powerbi,Powerbi Embedded,大家好。我可能不完全理解如何在我的Xamarin应用程序中嵌入PBI报告 我获得了我的访问令牌,并为此使用了主用户,但在我获得EmbedUrl并将其放入WebView的源程序中后,我收到一条消息:“请登录以查看此报告”。这是一个问题,因为我的客户没有PowerBi帐户,我希望他们能够看到此报告。问题可能在我用来获取访问令牌的头中,所以下面是我使用的代码 string url = "https://login.microsoftonline.com/" + tennant_id

大家好。我可能不完全理解如何在我的Xamarin应用程序中嵌入PBI报告 我获得了我的访问令牌,并为此使用了主用户,但在我获得EmbedUrl并将其放入WebView的源程序中后,我收到一条消息:“请登录以查看此报告”。这是一个问题,因为我的客户没有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>")
};

然后我得到我在WebView源属性中使用的嵌入url,但我没有登录。当我登录时,我会看到这个报告,但除此之外,我不能。我在哪里犯了错误?我是PowerBi嵌入的新手,所以请像向孩子解释一样解释它。

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

<WebView x:Name="WVtest" HeightRequest="300" WidthRequest="50" Navigated="WVtest_Navigated">
            
</WebView>
string url = "https://login.microsoftonline.com/" + tennant + "/oauth2/token";
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
这使得WebView知道如何将其源代码设置为index.html文件。它将加载这个.html并从中加载.js文件,以调用pbi客户端并显示pbi报告

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

<WebView x:Name="WVtest" HeightRequest="300" WidthRequest="50" Navigated="WVtest_Navigated">
            
</WebView>
string url = "https://login.microsoftonline.com/" + tennant + "/oauth2/token";
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
tennant是您的应用程序tennant的位置。对于body,我需要几个参数,如:

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>")
}
我将它们用作从上面发送到url的POST请求中的选项。作为对该请求的响应,我获得了access_令牌,这是我下一步唯一需要的东西。现在我确实收到了对powerBI API的请求:

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
字符串pbiUrl=”https://api.powerbi.com/v1.0/myorg/reports/";
client.DefaultRequestHeaders.Authorization=new System.Net.Http.Headers.AuthenticationHeaderValue(“Bearer”,accessToken)//客户端类型为HttpClient
现在我得到了我的embedUrl作为响应,我需要像这样反序列化:

<WebView x:Name="WVtest" HeightRequest="300" WidthRequest="50" Navigated="WVtest_Navigated">
            
</WebView>
string url = "https://login.microsoftonline.com/" + tennant + "/oauth2/token";
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
var response=wait client.GetStringAsync(pbiUrl);
var rese=JsonConvert.DeserializeObject(response2);
wait WVtest?.EvaluateJavaScriptAsync($“LoadEmbeddedObject(“{rese.embeddedUrl}”、“{accessToken}”、“0”、“{}”))//这里我用参数调用JavaScript函数
第一个参数是embedUrl,包含来自响应的链接,第二个参数是访问令牌,第三个参数是“0”,因为在ReportLoader.js中,将零转换为AAD类型的令牌,如果您使用的是嵌入令牌,请在此处使用“1”。最后一个参数是您的报告id。考虑到我在获取访问令牌时使用了它,我不知道为什么我必须再次使用它,但它必须在那里

现在它应该可以正常工作了。给它一点时间来加载,它真的很慢。如果您不喜欢报表的外观,请使用reportloader.js中的config对象,您在官方网站上有很好的文档
我真的希望这能帮助一些人,因为我有好几个星期都想不出来。如果您有任何更简单的解决方案,请张贴在这里,这只是一种对我有效的方法。请随意询问任何问题,我很乐意提供帮助。

您不能简单地在web浏览器中导航到embedURL。这是你正在传递给世界的东西。您必须在浏览器中加载网页,然后让客户端在此网页中的元素中加载报表。有关详细信息,请参阅。嗨,谢谢。我将尝试一下。介意检查一下我接收访问令牌的方式是否适合嵌入主用户的客户吗?我想是的。我相信这是一样的,ADAL/MSAL库将为您做什么。