<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: C# Power BI API 401 Unauthorized ServicePrincipalIsNotAllowedByTenantAdminSwitch in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/C-Power-BI-API-401-Unauthorized/m-p/1163802#M24201</link>
    <description>&lt;P&gt;Ok I solved this, my problem was with the RSL. (Roles)&lt;/P&gt;&lt;P&gt;My dataset, it was configure with a Dynamic Role, and I didn't know it. actually&amp;nbsp; just few Users were on that Role. so I was getting that error because that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was using on this line of code, which only works when your dataset is not Identity required.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;GenerateTokenRequest generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I resolve it?&lt;/P&gt;&lt;P&gt;1.- Make sure your Dataset is or not Identity Required(Using RLS).&amp;nbsp; Use the next two lines to evaluate:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;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)."
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If is Identity Required, then you will need the Username(email) and the Roles,&amp;nbsp; 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:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt; var rls = new EffectiveIdentity(Username, new List&amp;lt;string&amp;gt; { dataset.Id });
                if (!string.IsNullOrWhiteSpace(Roles))
                {
                    var rolesList = new List&amp;lt;string&amp;gt;();
                    rolesList.AddRange(Roles.Split(','));
                    rls.Roles = rolesList;
                }
                var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view", identities: new List&amp;lt;EffectiveIdentity&amp;gt; { rls });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Send&amp;nbsp; the&amp;nbsp; GenerateTokenRequestParameters variable to the function to get the EmbedToken,&lt;/P&gt;&lt;P&gt;as next:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;var TokenResponse = await client.Reports.GenerateTokenInGroupAsync(WorkspaceId, report.Id, generateTokenRequestParameters);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You should be good to go with those actions.&lt;/P&gt;</description>
    <pubDate>Tue, 16 Jun 2020 17:48:53 GMT</pubDate>
    <dc:creator>aromero</dc:creator>
    <dc:date>2020-06-16T17:48:53Z</dc:date>
    <item>
      <title>C# Power BI API 401 Unauthorized ServicePrincipalIsNotAllowedByTenantAdminSwitch</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/C-Power-BI-API-401-Unauthorized/m-p/1139779#M23998</link>
      <description>&lt;P&gt;I already have done all this steps:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;I have my Tenant account properly setup, &lt;A title="Steps to Setup the Tenant" href="https://docs.microsoft.com/en-us/power-bi/developer/embedded/embed-service-principal" target="_self"&gt;I have done all these steps. Done&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;Allow Service Principal to use the API Services. Done&lt;/LI&gt;&lt;LI&gt;Add Tenant account to the Security groups with all privilages to read and write. Done&lt;/LI&gt;&lt;LI&gt;Add the Account to the Workspace as an Admin. Done&lt;/LI&gt;&lt;LI&gt;Use the latest&lt;A title=" Pbi SDK v3." href="https://github.com/Microsoft/PowerBI-CSharp" target="_self"&gt; Pbi SDK v3.&lt;/A&gt; and use the latest Code samples. .Net Framework 4.8.&amp;nbsp; Done.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I sucessfully got a Token, but when I try to get the report (client.Reports.GenerateTokenInGroupAsync) I got the next error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;HTTP/1.1 401 Unauthorized
Content-Length: 0
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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my function to get the Token:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;private async Task&amp;lt;AuthenticationResult&amp;gt; DoAuthentication()
{
    AuthenticationResult authenticationResult = null;
    var AuthenticationType = _pbiSettings.AuthenticationType;
    var Tenant = _pbiSettings.TenantId;
    var ApplicationId = _pbiSettings.ApplicationId;
    var ClientId = _pbiSettings.ClientId;
    var ApplicationSecret = _pbiSettings.ClientSecret;
    string[] Scope = _pbiSettings.Scope.Split(';');
    var tenantSpecificURL = _pbiSettings.AuthorityUrl.Replace("organizations", Tenant);
    IConfidentialClientApplication clientApp = ConfidentialClientApplicationBuilder
                                                                    .Create(ClientId)
                                                                    .WithClientSecret(ApplicationSecret)
                                                                    .WithAuthority(tenantSpecificURL)
                                                                    .Build();
    try
    {
        authenticationResult = await clientApp.AcquireTokenForClient(Scope).ExecuteAsync();
    }
    catch (MsalException) { throw; }
    catch (Exception){ throw; }
    return authenticationResult;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This the code trying to get the report:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using (var client = new PowerBIClient(new Uri(_pbiSettings.ApiUrl), tokenCredentials))
{
    GenerateTokenRequest generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
    Report report=null;
    try
    {
        report = await client.Reports.GetReportInGroupAsync(new Guid(workspaceId), new Guid(reportId));
    }
    catch (Exception)
    {
        throw;
    }
    
    //Generate the Embed Token
    var TokenResponse =
        await client.Reports.GenerateTokenInGroupAsync(new Guid(workspaceId), report.Id, generateTokenRequestParameters);
    if (TokenResponse == null)
    {
        pbiEmbedReport.ErrorMsg = "Failed to generating embed Token";
        return pbiEmbedReport;
    }
    //Form the Result
    pbiEmbedReport.EmbedToken = TokenResponse;
    pbiEmbedReport.EmbedUrl = report.EmbedUrl;
    pbiEmbedReport.Id = report.Id.ToString();
    return pbiEmbedReport;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;***If I use the HttpClient Request, I sucessfully get the token and the report. I noted, that in the Post HttpClient Request I'm able to indicate Grant_type="password"&lt;/P&gt;&lt;P&gt;vs when I use the sdk, I noted that automaticaly set the grant_type to "client_credentials". and I get a 401 Error. not sure if that could be the problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What else could be failing ?&amp;nbsp; as far as I know I'm following all the documentation.&lt;/P&gt;&lt;P&gt;I also read all these posts:&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.powerbi.com/t5/Developer/Power-BI-API-return-401-Unauthorized-response/m-p/1084320#M23601" target="_blank" rel="noopener"&gt;https://community.powerbi.com/t5/Developer/Power-BI-API-return-401-Unauthorized-response/m-p/1084320#M23601&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.powerbi.com/t5/Developer/Embedding-Service-principle-AppOwnsData-401/m-p/699010" target="_blank" rel="noopener"&gt;https://community.powerbi.com/t5/Developer/Embedding-Service-principle-AppOwnsData-401/m-p/699010&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.powerbi.com/t5/Service/Custom-Connector-Unauthorized-401-Power-BI/m-p/819016#M80719" target="_blank" rel="noopener"&gt;https://community.powerbi.com/t5/Service/Custom-Connector-Unauthorized-401-Power-BI/m-p/819016#M80719&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2020 18:45:16 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/C-Power-BI-API-401-Unauthorized/m-p/1139779#M23998</guid>
      <dc:creator>aromero</dc:creator>
      <dc:date>2020-06-03T18:45:16Z</dc:date>
    </item>
    <item>
      <title>Re: C# Power BI API 401 Unauthorized ServicePrincipalIsNotAllowedByTenantAdminSwitch</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/C-Power-BI-API-401-Unauthorized/m-p/1140984#M24014</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;It looks like the error you are getting is due to the service principal not having access to the Power BI Service.&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can find how to enable that option in&amp;nbsp;&lt;A href="https://docs.microsoft.com/en-us/power-bi/developer/embedded/embed-service-principal" target="_blank" rel="noopener"&gt;Step 3&lt;/A&gt;&amp;nbsp; of the Microsoft Documentation.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please note that you need to enable "Allow service principals to use Power BI APIs".&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, it is highly recommended that the service principal that was created be added to a security group and then added within Step 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lastly, you will have to add the service principal to the workspace, not your personal workspace, that you would like for it to have access to. It need to be either a member or admin of that workspace.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jun 2020 07:39:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/C-Power-BI-API-401-Unauthorized/m-p/1140984#M24014</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-06-04T07:39:02Z</dc:date>
    </item>
    <item>
      <title>Re: C# Power BI API 401 Unauthorized ServicePrincipalIsNotAllowedByTenantAdminSwitch</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/C-Power-BI-API-401-Unauthorized/m-p/1163802#M24201</link>
      <description>&lt;P&gt;Ok I solved this, my problem was with the RSL. (Roles)&lt;/P&gt;&lt;P&gt;My dataset, it was configure with a Dynamic Role, and I didn't know it. actually&amp;nbsp; just few Users were on that Role. so I was getting that error because that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was using on this line of code, which only works when your dataset is not Identity required.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;GenerateTokenRequest generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I resolve it?&lt;/P&gt;&lt;P&gt;1.- Make sure your Dataset is or not Identity Required(Using RLS).&amp;nbsp; Use the next two lines to evaluate:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;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)."
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If is Identity Required, then you will need the Username(email) and the Roles,&amp;nbsp; 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:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt; var rls = new EffectiveIdentity(Username, new List&amp;lt;string&amp;gt; { dataset.Id });
                if (!string.IsNullOrWhiteSpace(Roles))
                {
                    var rolesList = new List&amp;lt;string&amp;gt;();
                    rolesList.AddRange(Roles.Split(','));
                    rls.Roles = rolesList;
                }
                var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view", identities: new List&amp;lt;EffectiveIdentity&amp;gt; { rls });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Send&amp;nbsp; the&amp;nbsp; GenerateTokenRequestParameters variable to the function to get the EmbedToken,&lt;/P&gt;&lt;P&gt;as next:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;var TokenResponse = await client.Reports.GenerateTokenInGroupAsync(WorkspaceId, report.Id, generateTokenRequestParameters);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You should be good to go with those actions.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 17:48:53 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/C-Power-BI-API-401-Unauthorized/m-p/1163802#M24201</guid>
      <dc:creator>aromero</dc:creator>
      <dc:date>2020-06-16T17:48:53Z</dc:date>
    </item>
    <item>
      <title>Re: C# Power BI API 401 Unauthorized ServicePrincipalIsNotAllowedByTenantAdminSwitch</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/C-Power-BI-API-401-Unauthorized/m-p/4617262#M59910</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;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:&lt;BR /&gt;&lt;BR /&gt;401 Unauthorized&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"X-PowerBI-Error-Info": "ServicePrincipalIsNotAllowedByTenantAdminSwitch",&lt;BR /&gt;"Strict-Transport-Security": "max-age=31536000; includeSubDomains",&lt;BR /&gt;"X-Frame-Options": "deny",&lt;BR /&gt;"X-Content-Type-Options": "nosniff",&lt;BR /&gt;"Access-Control-Expose-Headers": "RequestId,X-PowerBI-Error-Info",&lt;BR /&gt;"request-redirected": "true",&lt;BR /&gt;"home-cluster-uri": "&lt;A href="https://wabi-us-east2-redirect.analysis.windows.net/" target="_blank"&gt;https://wabi-us-east2-redirect.analysis.windows.net/&lt;/A&gt;",&lt;BR /&gt;"RequestId": "09d76868-3d43-4dac-a7cf-177e3136f89c",&lt;BR /&gt;"Date": "Wed, 19 Mar 2025 21:49:08 GMT",&lt;BR /&gt;"Content-Length": "0"&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;This is the http request that gives me the error mentioned above (obviously with my group and report id):&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://api.powerbi.com/v1.0/myorg/groups/my_group_id/reports/my_report_id" target="_blank"&gt;https://api.powerbi.com/v1.0/myorg/groups/my_group_id/reports/my_report_id&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;This is how looks the flow:&amp;nbsp;&lt;BR /&gt;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.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I already have done all this steps you mentioned:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;I have my Tenant account properly setup,&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A title="Steps to Setup the Tenant" href="https://docs.microsoft.com/en-us/power-bi/developer/embedded/embed-service-principal" target="_self" rel="nofollow noopener noreferrer"&gt;I have done all these steps. Done&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;Allow Service Principal to use the API Services. Done&lt;/LI&gt;&lt;LI&gt;Add Tenant account to the Security groups with all privilages to read and write. Done&lt;/LI&gt;&lt;LI&gt;Add the Account to the Workspace as an Admin. Done&lt;/LI&gt;&lt;LI&gt;Use the latest&lt;A title=" Pbi SDK v3." href="https://github.com/Microsoft/PowerBI-CSharp" target="_self" rel="nofollow noopener noreferrer"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Pbi SDK v3.&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and use the latest Code samples. .Net Framework 4.8.&amp;nbsp; Done.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;If you have any sugestion or modification I can do, let me know pls.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 22:39:55 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/C-Power-BI-API-401-Unauthorized/m-p/4617262#M59910</guid>
      <dc:creator>erickfernandez</dc:creator>
      <dc:date>2025-03-19T22:39:55Z</dc:date>
    </item>
  </channel>
</rss>

