Forum Discussion
C# Power BI API 401 Unauthorized ServicePrincipalIsNotAllowedByTenantAdminSwitch
Ok I solved this, my problem was with the RSL. (Roles)
My dataset, it was configure with a Dynamic Role, and I didn't know it. actually just few Users were on that Role. so I was getting that error because that.
I was using on this line of code, which only works when your dataset is not Identity required.
GenerateTokenRequest generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
How do I resolve it?
1.- Make sure your Dataset is or not Identity Required(Using RLS). Use the next two lines to evaluate:
if (dataset.IsEffectiveIdentityRequired == true){
//"This report is Identity Required. Make sure to Provide a Valid Username."
}
if (dataset.IsEffectiveIdentityRolesRequired == true) {
//Do your validations
//"This report is Identity Roles Required. Make sure to Provide a Valid Role(s)."
}
If is Identity Required, then you will need the Username(email) and the Roles, as far as I know Dynamic is the default Role, but the PowerBI Report developer should know this information. So do the next on code:
var rls = new EffectiveIdentity(Username, new List<string> { dataset.Id });
if (!string.IsNullOrWhiteSpace(Roles))
{
var rolesList = new List<string>();
rolesList.AddRange(Roles.Split(','));
rls.Roles = rolesList;
}
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view", identities: new List<EffectiveIdentity> { rls });
Send the GenerateTokenRequestParameters variable to the function to get the EmbedToken,
as next:
var TokenResponse = await client.Reports.GenerateTokenInGroupAsync(WorkspaceId, report.Id, generateTokenRequestParameters);
You should be good to go with those actions.
Hi,
I'm having the same problem. I'm creating a flow from Power Automate, and I've successfully made the HTTP requests to obtain the token.
After obtaining the token, when I try to validate my workspace properties to see these two properties you mentioned (IsEffectiveIdentityRequired, IsEffectiveIdentityRolesRequired), the API returns the error:
401 Unauthorized
{
"X-PowerBI-Error-Info": "ServicePrincipalIsNotAllowedByTenantAdminSwitch",
"Strict-Transport-Security": "max-age=31536000; includeSubDomains",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"Access-Control-Expose-Headers": "RequestId,X-PowerBI-Error-Info",
"request-redirected": "true",
"home-cluster-uri": "https://wabi-us-east2-redirect.analysis.windows.net/",
"RequestId": "09d76868-3d43-4dac-a7cf-177e3136f89c",
"Date": "Wed, 19 Mar 2025 21:49:08 GMT",
"Content-Length": "0"
}
This is the http request that gives me the error mentioned above (obviously with my group and report id):
https://api.powerbi.com/v1.0/myorg/groups/my_group_id/reports/my_report_id
This is how looks the flow:
In the HTTP2 i can get the token, after that I parse the response to get the token value out. Finally I use that token to try to se my report's properties but I get the 401 error.
I already have done all this steps you mentioned:
- I have my Tenant account properly setup, I have done all these steps. Done
- Allow Service Principal to use the API Services. Done
- Add Tenant account to the Security groups with all privilages to read and write. Done
- Add the Account to the Workspace as an Admin. Done
- Use the latest Pbi SDK v3. and use the latest Code samples. .Net Framework 4.8. Done.
If you have any sugestion or modification I can do, let me know pls.