PowerBI with .NET Core

In May I integrated PowerBI in a .NET Core and Angular application. In June a new version of PowerBI was released with an entire new way of getting reports to your customers, so I had to do the exercise again. The C# SDK only supports the .NET framework so I used the PowerBI API directly to implement it. Although the API is documented I was only able to get it all working by reading it carefully and also following every step mentioned in this article. The end result is great, Power BI offers a great reporting solution, getting it to run however was not a walk in the park. You can read about different scenarios on the official website.

The ability to alter and publish reports without writing any code is awesome. In my current project we have an admin user that creates reports in PowerBI desktop, he uploads the report in our web application and we then publish it to each customer. They all have their own database and PowerBI allows you to basically change the connection string. In theory, it’s mentioned on the PowerBI site, we could also allow the customers to modify the published report or create their own. A scenario which we currently don’t need but which I might investigate nonetheless.

This weekend I decided to take a look again at the C# SDK. It’s really just a small layer between the API and your application and you don’t really need it, but it’s the natural place to start looking around. Since it only targets the .NET framework I couldn’t use it in .NET core. I forked the project, modified the csproj files to target .NET Standard 2.0 and updated the packages that were referenced. Zero warnings, zero errors. Not bad for a conversion!

The only thing that is different from the official samples is the way to get a token to call functions in the API. The official samples use UserPasswordCredentials which is not available. So as another Github user mentioned you need to do the heavy lifting yourself.

private async Task<TokenCredentials> GetAccessToken()
    using (HttpClient client = new HttpClient())
        string tenantId = "";
        var tokenEndpoint = "";
        var accept = "application/json";
        var userName = "";
        var password = "";
        var clientId = "";
        client.DefaultRequestHeaders.Add("Accept", accept);
        string postBody = null;
        postBody = $@"resource=https%3A%2F%2Fanalysis.windows.net/powerbi/api
        var tokenResult = await client.PostAsync(tokenEndpoint, new StringContent(postBody, Encoding.UTF8, "application/x-www-form-urlencoded"));
        var tokenData = await tokenResult.Content.ReadAsStringAsync();
        JObject parsedTokenData = JObject.Parse(tokenData);
        var token = parsedTokenData["access_token"].Value<string>();
        return new TokenCredentials(token, "Bearer");

Once you have the token you can use the SDK as is.

    var tokenCredentials = await GetAccessToken();
    using (var powerBiclient = new PowerBIClient(new Uri("https://api.powerbi.com/"), tokenCredentials))
        var reports = powerBiclient.Groups.GetGroups();

I’ve pushed the code to GitHub and published it as a NuGet package.