cancel
Showing results for 
Search instead for 
Did you mean: 
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
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

BizApps LATAM 2023

Business Application LATAM Summit 2023

Join the biggest FREE Business Applications Event in LATAM this February.

Power Platform Bootcamp

Global Power Platform Bootcamp

In this bootcamp we will deep-dive into Microsoft’s Power Platform stack with hands-on sessions and labs, delivered to you by experts and community leaders.