Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
I am trying to implement a C# Azure Function App to use the PowerBI Rest API to extract information about workspaces etc in our tenant.
As a starting point I used Jeff Pries's excellent blog post:
Initially I implemented this as a standalone local app and it worked fine. So I know that I have setup my App Registration correctly in the Azure AD and I know that I can successfully authorize non-interactively with saved User/Password credentials and obtain a list of PowerBI workspaces on our tenant.
However, when I came to implement this in an Azure Function App I found that the option to use User/Password isn't available, so instead am trying using a ClientSecret to authenticate, but cannot get it to work.
I am using AcquireTokenAsync method to get the auth token:
authResult = await authContext.AcquireTokenAsync(ResourceURL, clientCredential).ConfigureAwait(false);
I know I am using the correct client secret because if I deliberately use the wrong one I get an "Invalid client secret is provided" exception.
I then CreateAuthorizationHeader method to create the authorization header
authToken = authResult.Result.CreateAuthorizationHeader();
You can see from the console screenshot below that this all appears to work fine. I end up with a sound looking bearer token:
Then I create an HTTPClient and set the auth token in the Authorization header:
client = new HttpClient();
client.DefaultRequestHeaders.UserAgent.ParseAdd(HTTPHEADUSERAGENT);
client.DefaultRequestHeaders.Add("Authorization", authToken);
Finally, I try to use the client to call the PowerBI API:
string serviceURL ="https://api.powerbi.com/v1.0/myorg/groups";
response = client.GetAsync(serviceURL).Result;
Every time I get response code: Unauthorized.
What am I doing wrong?
The ResourceURL I am using to acqure the token is: "https://analysis.windows.net/powerbi/api".
I have tried using different versions of the Microsoft.IdentityModel.Clients.ActiveDirectory. I tried using the latest v5.2.8, but I have also seen posts which suggest the problem could be solved by using v3.19.8. But neither makes any difference.
The AuthorityURL I am using to get the auth context for v3.19.8 is: "https://login.windows.net/common/oauth2/authorize".
The AuthorityURL I am using to get the auth context for v5.2.8 was: "https://login.microsoftonline.com/common". I also tried using URLs that include our tenant id.
HI @joolzfawcett,
Maybe you can try to refer to the following document sample codes to replace the get token parts and test to confirm if this token works on your side.
Step 2: Get an authentication access token
Please also take a look at the best practice of azure function if it helps:
Best practices for performance and reliability of Azure Functions
Regards,
Xiaoxin Sheng
Check out the July 2025 Power BI update to learn about new features.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
User | Count |
---|---|
7 | |
7 | |
3 | |
2 | |
2 |
User | Count |
---|---|
6 | |
5 | |
4 | |
4 | |
4 |