Forum Discussion
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.Password = "password";
ODataFolders result = apiInstance.GetFolders(top, skip, filter, count, orderBy, select);This results in Error calling GetFolders: Unable to connect to the remote server...
- Anonymous8 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.
3 Replies
- mgmeyer
Power BI Team
If your using the code generated from Swagger you need to us the NtlmAuthenticator, setting user name and password with likely use basic auth:
client.Configuration.ApiClient.RestClient.Authenticator = new NtlmAuthenticator(CredentialCache.DefaultCredentials);
- AnonymousNot applicable
Thanks! Though I still get the error message Unable to connect to the remote server. I tried this:
FoldersApi client = new FoldersApi("http://server/reports/api/v2.0/Folders");But I get the same error again. Both URLs gives me some JSON in Chrome.
- AnonymousNot applicable
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.