Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

Power BI REST API authentication

How do I authenticate the user when calling the REST API?   FoldersApi client = new FoldersApi("http://server/reports/api/v2.0"); client.Configuration.Username = "usernamne"; client.Configuration.P...
  • Anonymous's avatar
    Anonymous
    8 years ago

    I solved the problem by just using RestSharp and Newtonsoft.Json. I created a ASP.Net Core API project and this is the code for my controller.

     

    using System.Collections.Generic;
    using Microsoft.AspNetCore.Mvc;
    using RestSharp;
    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json;
    
    namespace PortalAPI.Controllers
    {
        [Produces("application/json")]
        [Route("api/Reports")]
        public class ReportsController : Controller
        {
            private static string PBI_REST_API_URL = "http://myserver/reports/api/v2.0";
    
            private static string PBI_REPORTS_URL = "http://myserver/reports/powerbi";
    
            // GET api/values
            [HttpGet]
            public string Get()
            {
                var client = new RestClient(PBI_REST_API_URL)
                {
                    Authenticator = new RestSharp.Authenticators.NtlmAuthenticator()
                };
                RestRequest reportsRequest = new RestRequest("/CatalogItems", Method.GET);
    
                var response = client.Execute(reportsRequest);
    
                var svar = JObject.Parse(response.Content);
    
                var reportList = (JArray)svar["value"];
    
                var resultReportList = new List<Report>();
    
                foreach(JObject report in reportList)
                {
                    if (((string)report["@odata.type"]).Equals("#Model.PowerBIReport"))
                    {
                        resultReportList.Add(new Report { Name = (string)report["Name"], Path = PBI_REPORTS_URL + (string)report["Path"] });
                    }
                }
    
                return JsonConvert.SerializeObject(resultReportList); ;
            }
        }
    
        public class Report
        {
            public string Name { get; set; }
            public string Path { get; set; }
        }
    }

    Since I dont need all values from the returnd JSON it was very conveniant to just pick the stuff I needed with Newtonsoft.Json. The service will have to be run with an account that has access to PBI Report Server. Hope this helps someone.