Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin 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.
I'm writing a C# program to perform administrative tasks in Power BI Embedded such as creating, updating, and deleting workspaces, datasets, and reports. I am using the Nuget library "Microsoft.PowerBI.Api Version=3.31.2". It appears I need to create an instance of the Microsoft.PowerBI.Api.PowerBIClient. I want it to authenticate using the credentials of an AAD registered application (not the credentials of a user). I've created an AAD app registration and given it permission to call the entire Power BI API. I have the application's clientid, client secret, and tenantid. But I can't figure out which PowerBIClient constructor to use so that it can utilize the credentials of my AAD application. Apparently I need to pass a ServiceClientCredentials object to the PowerBIClient constructor, but ServiceClientCredentials is an abstract class, and I can't figure out how to use the application's clientid, client secret, and tenantid to construct something that derives from ServiceClientCredentials. The only examples I found searching online use ADAL, but that's deprecated, so if an external library is needed I want to use MSAL. Can someone please show me a code sample showing how to create a PowerBIClient using the credentials of an AAD app, using MSAL if necessary. Thank you.
Solved! Go to Solution.
I found the answers in this 30 minute video from SqlBi: Creating a service principal account for Power BI API - SQLBI
I feel flattered that Marco Russo was stumped on this for two days, while I only wasted one. 😀 Microsoft, it would sure be good if the documentation explained this.
Actually, we don't need to use any library to get the access token for PowerBIClient.
We can just use the HttpClient and send normal HTTP Request, like this:
var clientHandler = new HttpClientHandler
{
UseCookies = false,
};
var client = new HttpClient(clientHandler);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://login.microsoftonline.com/<your_tenant_id>/oauth2/v2.0/token"),
Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{ "client_id", "<your_client_id>" },
{ "scope", "https://analysis.windows.net/powerbi/api/.default" },
{ "client_secret", "<your_client_secret>" },
{ "grant_type", "client_credentials" },
}),
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}
More details: https://kalcancode.wordpress.com/2025/02/18/powerbiclient-how-to-get-access-token/
Cheers,
Kim Anh
I found the answers in this 30 minute video from SqlBi: Creating a service principal account for Power BI API - SQLBI
I feel flattered that Marco Russo was stumped on this for two days, while I only wasted one. 😀 Microsoft, it would sure be good if the documentation explained this.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
4 | |
3 | |
3 | |
3 | |
3 |