0

Good day. I have a problem with displaying a PowerBi report in Xamarin.Forms app I'm making (newbie in PBI here). I only need to view report, I don't have to edit stuff. I got access token and after that I did a GET request and got embedded url from PowerBi which I used in WebView.Source to display on screen. And it loads to that report BUT it says I don't have permissions to view report which I should have (user I used for token is PBI PRO user). Text I get is "To view this report, ask the author for access". Here is token POST code

string url = "https://login.microsoftonline.com/<pbi_tenant_id>/oauth2/token";

                    IEnumerable<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>()
                    {
                        new KeyValuePair<string, string>("name",            "testName"),
                        new KeyValuePair<string, string>("grant_type",      "password"),
                        new KeyValuePair<string, string>("scope",           "openid"),
                        new KeyValuePair<string, string>("resource",        "https://analysis.windows.net/powerbi/api"),
                        new KeyValuePair<string, string>("client_id",       "<client_id>"),
                        new KeyValuePair<string, string>("username",        "<pro_user>"),
                        new KeyValuePair<string, string>("password",        "<password>"),
                        new KeyValuePair<string, string>("client_secret",   "<client_secret>")
                        };
HttpContent content = new FormUrlEncodedContent(parameters);

After all this I got my token, and I made GET request with that token to get embedded url

using (HttpResponseMessage response = await client.PostAsync(url, content))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            using (HttpContent contentOfReponse = response.Content)
                            {
                                try
                                {
                                    string tokenResponseJSON = await contentOfReponse.ReadAsStringAsync(); //token in json format that I needed to convert to string in next line
                                    var tokenRepsonse = JsonConvert.DeserializeObject<TokenInfoModel>(tokenResponseJSON);

                                    string accessToken = tokenRepsonse.access_token; //my string token

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

                                        var response2 = await client.GetStringAsync(pbiUrl);

                                        var rese = JsonConvert.DeserializeObject<PBIRepost>(response2);
                                        PbiEmbedUrl = rese.embedUrl + "&autoAuth=true" + "&isOwnedByMe=" + rese.isOwnedByMe + "apps/<app_id>";
                                        RaisePropertyChanged(PbiEmbedUrl); 
                                    }
                                    catch (Exception ex)
                                    {

                                        //await App.Current.MainPage.DisplayAlert();
                                    }   
                                }
                                catch (Exception)
                                {
                                    //await App.Current.MainPage.DisplayAlert();
                                }
                            }
                        }
                        else
                        {
                            //await App.Current.MainPage.DisplayAlert();
                        }
                    }

All of this works, I got url and used it in WebView which opens to PBI website, but displays "To view this report, ask the author for access". Now something that may be really important, on my phone I am NOT a pro PBI user, but that should not be a problem because I used PBI PRO user for token. If that is a problem, how could I do it without being PRO user?

Embed url I got is: https://app.powerbi.com/reportEmbed?reportId=<report_id>&config=VST1BFLUItUFJeyJjbHVzdGVy5ldCIsImVtYmVkRmllZFRlbGVtZXWRpcmVjdC5hbmFseXNpcy53aW5kb3dzLm5ldCIsImVtYmVkRmVhdHVyZXMiOnsibRyeHVzdGVyVXJsIjoiaHR0cUVtYmVkIjp0cnVlfX0%3d" (not a real config string, not sure I can share real one, so I made it up for this question)

I did not mess with config in embedded url so that could also be the problem, but I don't know how could I edit config.

  • 1
    You can't simply navigate to the embedURL in a web browser. It is something, that you are passing to the [Power BI client](https://github.com/Microsoft/PowerBI-JavaScript). You must load a web page in the browser and then let the client to load the report in a `
    ` element in this page. See [this answer](https://stackoverflow.com/questions/56409362/is-there-any-way-to-embed-power-bi-reports-and-dashboards-in-vb-net-or-c-sharp-d/56418991#56418991) for step-by-step details.
    – Andrey Nikolov May 07 '21 at 08:11
  • @AndreyNikolov Hi, thanks for the answer. I'm kinda confused because this Power BI client you linked is for JavaScript, not Xamarin. I searched everywhere for something similar for Xamarin, but found nothing. I think I read somewhere that you can import JS file in WebView, so I will try that out. If you know any documentation for PBI embedding into Xamarin I would be very grateful to you. – Andreja Živanović May 10 '21 at 13:29
  • Every possible way to embed Power BI boils down to an iFrame in an web page shown in a browser. It doesn’t matter is your app a web app, or a desktop app, or a mobile one - it is something shown in the browser. Read again the linked question and both answers. They explains everything in details (also read the given link for embedding in a WPF application). – Andrey Nikolov May 10 '21 at 14:29

0 Answers0