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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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