Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

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...

1 ACCEPTED SOLUTION
Anonymous
Not 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.

View solution in original post

3 REPLIES 3
mgmeyer
Power BI Team
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);

Anonymous
Not 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.

Anonymous
Not 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.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.