<?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: Service Principal Credential Configuration Error: PowerBiNotAuthorized in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4730621#M61778</link>
    <description>&lt;P&gt;I fixed this issue by &lt;A href="https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/bind-to-gateway-in-group" target="_self"&gt;binding&lt;/A&gt; the dataset to the same gateway and datasource after taking over as the service principal. However, I now get a bad request error with all different credential types I tried with (Basic, Key, and OAuth2)&lt;/P&gt;</description>
    <pubDate>Thu, 12 Jun 2025 19:13:00 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2025-06-12T19:13:00Z</dc:date>
    <item>
      <title>Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4729086#M61754</link>
      <description>&lt;P&gt;Hi.&lt;/P&gt;&lt;P&gt;I have a similar problem to the following forum posted by &lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/325592"&gt;@akarkal&lt;/a&gt;&amp;nbsp;:&amp;nbsp;&lt;A href="https://community.fabric.microsoft.com/t5/Developer/Update-DataSet-Credentials-using-Service-Principal/m-p/2236065" target="_blank" rel="noopener"&gt;Solved: Update DataSet Credentials using Service Principal - Microsoft Fabric Community&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Similarly, I am trying to update datasource credentials of a dataset using a service principal through the C# PowerBI Client Library.&amp;nbsp;&lt;BR /&gt;However, no fixes on my part have worked.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the setup:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN&gt;tenant settings has the toggle enabled for security group in which the service principal is a member of&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;Service Principal is an admin of the workspace&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;Service Principal takes over the dataset inside the C# script&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;Service Principal has Dataset.ReadWrite.All and Gateway.ReadWrite.All&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;The datasource that I want to change is a cloud datasource (Azure Databricks). As such, it is using a cloud gateway.&amp;nbsp;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;I am trying to update the credentials through KEY credentials.&amp;nbsp;&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;SPAN&gt;Here is the 2 main parts of my script after getting an authenticated Power BI Client:&lt;BR /&gt;&lt;STRONG&gt;Getting the Key (PAT):&lt;/STRONG&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;var secretClient = new SecretClient(new Uri(keyVaultUri), new DefaultAzureCredential());
KeyVaultSecret secret = await secretClient.GetSecretAsync(secretName, secretVersion);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;Trying to update datasource:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;var datasources = (await pbiClient.Datasets.GetDatasourcesAsAdminAsync(datasetId)).Value;
var datasource = datasources.First();

var gatewayId = datasource.GatewayId ?? throw new InvalidOperationException("Datasource is not associated with a gateway.");
var datasourceId = datasource.DatasourceId ?? throw new InvalidOperationException("Datasource does not have a DatasourceId.");

var credentials = new KeyCredentials(key);

var credentialDetails = new CredentialDetails(
                credentials,
                PrivacyLevel.None,
                EncryptedConnection.NotEncrypted
            );

await pbiClient.Datasets.TakeOverAsync(workspaceId, datasetId);
await pbiClient.Gateways.UpdateDatasourceAsync(gatewayId, datasourceId, new UpdateDatasourceRequest(credentialDetails));&lt;/LI-CODE&gt;&lt;P&gt;At the last line of code, I get the &lt;STRONG&gt;PowerBINotAuthorized&amp;nbsp;&lt;/STRONG&gt;error.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also tried doing a basic authentication where the username is the SP's ClientId and the password is the SP's password. However, I got the following error:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Failed to update datasource: One or more errors occurred.
Inner Exception: {
  "code": "BadRequest",
  "message": "Bad Request",
  "details": [
    {
      "message": "Invalid value",
      "target": "datasourceDelta"
    }
  ]
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;This error might be because&amp;nbsp;&lt;STRONG&gt;Basic&lt;/STRONG&gt; credentials might not work with Service Principals.&amp;nbsp;&lt;BR /&gt;I haven't really found a solution even with the related forum. As such, any help would be appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jun 2025 18:12:47 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4729086#M61754</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-06-11T18:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4729587#M61760</link>
      <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;You're getting the PowerBiNotAuthorized error likely because Azure Databricks credentials cannot be updated using a service principal with KeyCredentials — it doesn’t work that way for cloud datasources like Databricks.&lt;/P&gt;
&lt;P&gt;Here’s what usually works, try this, use Basic credentials (not KeyCredentials), and pass the Databricks Personal Access Token (PAT) as the password. &lt;BR /&gt;Like this, In Csharp use - var credentials = new BasicCredentials("token", secret.Value); // "token" can be any string&lt;/P&gt;
&lt;P&gt;And then same in csharp use - var credentialDetails = new CredentialDetails(credentials, PrivacyLevel.Private,EncryptedConnection.Encrypted).&lt;/P&gt;
&lt;P&gt;And make sure that your service principal is allowed in Admin Portal → Tenant Settings → Developer settings. After calling TakeOverAsync, fetch the datasources again before updating&lt;/P&gt;
&lt;P&gt;And remember this, Use BasicCredentials, not KeyCredentials.&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Use PAT as password, and any dummy username like "token".&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Confirm SP permissions and tenant settings.&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Call TakeOverAsync, then get datasources again before updating&lt;BR /&gt;&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;&lt;SPAN&gt;If this response helps, consider marking it as&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;“Accept as solution”&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;and giving a&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;“kudos”&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;to assist other community members.&lt;BR /&gt;&lt;/SPAN&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Akhil.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jun 2025 06:52:26 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4729587#M61760</guid>
      <dc:creator>v-agajavelly</dc:creator>
      <dc:date>2025-06-12T06:52:26Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4730234#M61774</link>
      <description>&lt;P&gt;Hi, thank you so much for the lead. However, after the service principal takes over the dataset, the datasource that I get has no datasourceId nor a gatewayId. Relevant code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;var datasource = (await pbiClient.Datasets.GetDatasourcesAsAdminAsync(datasetId)).Value.First();

await pbiClient.Datasets.TakeOverAsync(workspaceId, datasetId);

datasource = (await pbiClient.Datasets.GetDatasourcesAsAdminAsync(datasetId)).Value.First();
var gatewayId = datasource.GatewayId ?? throw new InvalidOperationException("Datasource is not associated with a gateway."); // errors with no gateway id
var datasourceId = datasource.DatasourceId ?? throw new InvalidOperationException("Datasource does not have a DatasourceId."); // errors with no datasource id

Console.WriteLine($"Gateway ID: {gatewayId}, Datasource ID: {datasourceId}");

await pbiClient.Gateways.UpdateDatasourceAsync(gatewayId, datasourceId, new UpdateDatasourceRequest(credentialDetails));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jun 2025 12:33:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4730234#M61774</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-06-12T12:33:28Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4730621#M61778</link>
      <description>&lt;P&gt;I fixed this issue by &lt;A href="https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/bind-to-gateway-in-group" target="_self"&gt;binding&lt;/A&gt; the dataset to the same gateway and datasource after taking over as the service principal. However, I now get a bad request error with all different credential types I tried with (Basic, Key, and OAuth2)&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jun 2025 19:13:00 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4730621#M61778</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-06-12T19:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4731667#M61801</link>
      <description>&lt;P&gt;Hey&amp;nbsp;@Anonymous&lt;/a&gt;&amp;nbsp; Glad to hear you made progress and figured out that the dataset needed to be bound again after the service principal took over, that's a tricky step that often gets missed.&lt;/P&gt;
&lt;P&gt;Now, about the BadRequest error you're seeing when trying to update the credentials: I've run into the exact same issue before when working with Azure Databricks and a service principal, and here's what finally worked for me (after lots of trial and error!).&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;What finally solved it is, first off, don’t use KeyCredentials for Databricks. It just doesn't work. Instead, go with BasicCredentials, but here's the trick — for the username, just use something like "token", and for the password, use the actual Databricks Personal Access Token (PAT) you fetched from Key Vault.&lt;/P&gt;
&lt;P&gt;So basically in csharp &lt;BR /&gt;var credentials = new BasicCredentials("token", patFromKeyVault);&lt;BR /&gt;var credentialDetails = new CredentialDetails(credentials, PrivacyLevel.Private, EncryptedConnection.Encrypted);&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;***Now here’s the important bit: after you do the TakeOverAsync, you must rebind the dataset to the gateway again &lt;EM&gt;before&lt;/EM&gt; you try updating the credentials. Even if the gateway looks the same, it won’t “stick” unless you explicitly rebind.&lt;/P&gt;
&lt;P&gt;In csharp try the below&lt;/P&gt;
&lt;P&gt;await pbiClient.Datasets.BindToGatewayAsync(datasetId, new BindToGatewayRequest { GatewayObjectId = gatewayId });&lt;/P&gt;
&lt;P&gt;After that, fetch the data sources again don’t reuse the earlier result because now the binding is fresh and should return a valid datasourceId and gatewayId.&lt;/P&gt;
&lt;P&gt;Then update the credentials using those refreshed IDs.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;So the full flow that worked for me was.&lt;/LI&gt;
&lt;LI&gt;Take over the dataset with the service principal&lt;/LI&gt;
&lt;LI&gt;Rebind the dataset to the cloud gateway&lt;/LI&gt;
&lt;LI&gt;Re-fetch the datasource info&lt;/LI&gt;
&lt;LI&gt;Use BasicCredentials with "token" and the actual PAT&lt;/LI&gt;
&lt;LI&gt;Update the datasource&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Make sure the service principal is allowed in the Tenant Settings &amp;gt; Developer Settings, and that it has all the right API permissions (you’ve likely done this, but just worth double-checking).&lt;BR /&gt;&lt;BR /&gt;---------------------------------------------------------------------------------------------------------&lt;BR /&gt;&lt;SPAN&gt;If this response helps, consider marking it as&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;“Accept as solution”&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;and giving a&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;“kudos”&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;to assist other community members.&lt;BR /&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Akhil.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jun 2025 14:02:07 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4731667#M61801</guid>
      <dc:creator>v-agajavelly</dc:creator>
      <dc:date>2025-06-13T14:02:07Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4731900#M61806</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/1283468"&gt;@v-agajavelly&lt;/a&gt;&amp;nbsp;. Thank you for your response again. Actually those are all what I did. However, I still get bad request. That might be because I am just using the wrong PAT or clientId so I'll check on my end. Here is my C# script:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using var pbiClient = await PowerBIAuth.GetPowerBIClientAsync();
var key = await PowerBIAuth.GetPAT();

var credentials = new BasicCredentials("token", key);

var credentialDetails = new CredentialDetails(
                credentials,
                PrivacyLevel.Private,
                EncryptedConnection.Encrypted
            );

//=================MAIN LOGIC===================

var datasources = (await pbiClient.Datasets.GetDatasourcesAsAdminAsync(datasetId)).Value;
var datasource = datasources.First();

var gatewayId = datasource.GatewayId ?? throw new InvalidOperationException("Datasource is not associated with a gateway.");
var datasourceId = datasource.DatasourceId ?? throw new InvalidOperationException("Datasource does not have a DatasourceId.");

await pbiClient.Datasets.TakeOverAsync(groupId, datasetId);
await pbiClient.Datasets.BindToGatewayInGroupAsync(groupId, datasetId, new BindToGatewayRequest(gatewayId, [datasourceId]));

//Get the datasource again after binding to ensure it has the correct gateway and datasource ID
datasources = (await pbiClient.Datasets.GetDatasourcesAsAdminAsync(datasetId)).Value;
datasource = datasources.First();

gatewayId = datasource.GatewayId ?? throw new InvalidOperationException("Datasource is not associated with a gateway.");
datasourceId = datasource.DatasourceId ?? throw new InvalidOperationException("Datasource does not have a DatasourceId.");

await pbiClient.Gateways.UpdateDatasourceAsync(gatewayId, datasourceId, new UpdateDatasourceRequest(credentialDetails));&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 13 Jun 2025 20:08:29 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4731900#M61806</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-06-13T20:08:29Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4734106#M61833</link>
      <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Thanks for sharing your code and walking through the full flow—I really appreciate that level of detail! From what I see, everything looks wired up correctly.&lt;BR /&gt;That said, since you're still getting a BadRequest at the UpdateDatasourceAsync step, step, I think we might be dealing with one of two things.&lt;BR /&gt;&lt;BR /&gt;First try The Personal Access Token (PAT) might not be what Power BI is expecting.&lt;BR /&gt;Just to double-check, is the key you're retrieving here via await PowerBIAuth.GetPAT() definitely a Databricks Personal Access Token? It should be.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Created from your Databricks workspace (under your profile settings → Access tokens)&lt;/LI&gt;
&lt;LI&gt;Active and not expired&lt;/LI&gt;
&lt;LI&gt;Belonging to a user that has access to the cluster or SQL endpoint you're connecting to&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The reason I’m asking is, if you're using the service principal's clientId or clientSecret here instead of a Databricks PAT, Power BI will quietly reject the credentials (even though the structure looks right) and throw a vague BadRequest error.&lt;/P&gt;
&lt;P&gt;Also, tiny thing: you can test if the PAT is working using a tool like Azure Data Studio or DBeaver — just plug in:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Username: "token"&lt;/LI&gt;
&lt;LI&gt;Password: your actual PAT&lt;BR /&gt;That’s a quick way to confirm the token itself is good before going through the script again.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Once Check what kind of datasource Power BI thinks you're dealing with.&lt;/P&gt;
&lt;P&gt;After binding, try logging the details of the datasource like this in csharp.&lt;/P&gt;
&lt;P&gt;Console.WriteLine(JsonConvert.SerializeObject(datasource, Formatting.Indented));&lt;/P&gt;
&lt;P&gt;You're looking for something like json?&lt;/P&gt;
&lt;P&gt;"datasourceType": "Extension",&lt;/P&gt;
&lt;P&gt;"connectionDetails": {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "server": "&amp;lt;your-databricks-url&amp;gt;",&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "database": "&amp;lt;your-db-name&amp;gt;"&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;Sometimes the dataset might be pointing to a slightly different type of source (depending on how it was originally created), and that could block credential updates.&lt;/P&gt;
&lt;P&gt;Please try updating credentials manually in Power BI Service, Just to isolate the issue, go to Power BI web portal -- dataset settings -- edit credentials -- and try using:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Username: token&lt;/LI&gt;
&lt;LI&gt;Password: your Databricks PAT&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;SPAN&gt;---------------------------------------------------------------------------------------------------------&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If this response helps, consider marking it as&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;“Accept as solution”&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;and giving a&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;“kudos”&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;to assist other community members.&lt;BR /&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Akhil.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jun 2025 01:57:40 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4734106#M61833</guid>
      <dc:creator>v-agajavelly</dc:creator>
      <dc:date>2025-06-17T01:57:40Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4734833#M61845</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/1283468"&gt;@v-agajavelly&lt;/a&gt;.&amp;nbsp;Would you know how to manually change as a service principal? I am not sure how to log in to Power BI Service as a Service Principal.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jun 2025 13:11:41 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4734833#M61845</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-06-17T13:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4734859#M61846</link>
      <description>&lt;P&gt;For your reference, this is the code for `GetPAT()`&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static async Task&amp;lt;string&amp;gt; GetPAT()
{
    const string secretName = "";
    var secretClient = new SecretClient(new Uri(keyVaultUri), new DefaultAzureCredential());
    KeyVaultSecret secret = await secretClient.GetSecretAsync(secretName);

    return secret.Value;
}&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 17 Jun 2025 13:35:57 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4734859#M61846</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-06-17T13:35:57Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4735344#M61851</link>
      <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;You can’t log into the Power BI Service (web portal) as a service principal in the usual interactive way (like you would with a regular user account). Service principals are meant to be used programmatically&amp;nbsp; meaning only via APIs or SDKs and don’t have a UI login experience.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;So when I suggested trying to update the credentials manually&amp;nbsp; please try in the Power BI Service, that would need to be done using a regular user account that has access to the same dataset and workspace.&lt;/P&gt;
&lt;P&gt;Here’s what I’d suggest.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Log in to Power BI Service using a user account that:&lt;/LI&gt;
&lt;UL&gt;
&lt;LI&gt;Has Admin or Member access to the workspace the dataset lives in&lt;/LI&gt;
&lt;LI&gt;Ideally is the same account that originally created the dataset (or has taken it over)&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI&gt;Go to the dataset settings → Data source credentials&lt;/LI&gt;
&lt;LI&gt;Try entering the credentials manually.&lt;/LI&gt;
&lt;UL&gt;
&lt;LI&gt;Username: "token"&lt;/LI&gt;
&lt;LI&gt;Password: (the same Databricks PAT you're pulling from Key Vault)&lt;/LI&gt;
&lt;/UL&gt;
&lt;/OL&gt;
&lt;P&gt;If that works, then awesome — it confirms the token is valid and the dataset can accept it.&lt;/P&gt;
&lt;P&gt;If it fails, then at least the UI might show you a clearer error like “invalid credentials” or “cannot connect,” which can be much easier to debug than a generic BadRequest from the API.&lt;/P&gt;
&lt;P&gt;If Your GetPAT() method looks perfect — it’s just important to make sure that:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The secretName points to the Databricks PAT in Key Vault (and not the SP client secret by mistake)&lt;/LI&gt;
&lt;LI&gt;The Key Vault secret is actually the token string (no quotes or extra formatting).&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;SPAN&gt;---------------------------------------------------------------------------------------------------------&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If this response helps, consider marking it as&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;“Accept as solution”&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;and giving a&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;“kudos”&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;to assist other community members.&lt;BR /&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Akhil.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jun 2025 04:25:15 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4735344#M61851</guid>
      <dc:creator>v-agajavelly</dc:creator>
      <dc:date>2025-06-18T04:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4735955#M61868</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/1283468"&gt;@v-agajavelly&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am getting an error when I manually use the PAT so I think I will check if the PAT is right.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jun 2025 12:28:12 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4735955#M61868</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-06-18T12:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4736881#M61887</link>
      <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Just wanted to check in did you get a chance to verify if the PAT you're using is valid? Sometimes it’s something small like a missing permission or an expired token that trips things up.&lt;/P&gt;
&lt;P&gt;If it’s still giving you trouble, feel free to share what error you’re seeing, happy to help you troubleshoot it.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Akhil&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jun 2025 06:56:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4736881#M61887</guid>
      <dc:creator>v-agajavelly</dc:creator>
      <dc:date>2025-06-19T06:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4742310#M61968</link>
      <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Just checking in one last time. Were you able to try out any of the suggestions shared earlier? &lt;STRONG&gt;If your issue is resolved, marking the accepted solution would be a big help to others who might be facing the same scenario.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;If you went in a different direction or still need support, feel free to drop a quick update, we’re happy to keep helping.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Akhil.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jun 2025 12:02:10 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4742310#M61968</guid>
      <dc:creator>v-agajavelly</dc:creator>
      <dc:date>2025-06-24T12:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: Service Principal Credential Configuration Error: PowerBiNotAuthorized</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4746543#M62027</link>
      <description>&lt;P&gt;Here is the script that finally worked for me. I was suggested to get the OAuth2 access token in some other way and I did it that way. I am not sure how to tell from the power bi service if the data source is actually authenticated. However, I am assuming that it is authenticated because the script does not output an error.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;# Set execution policy for Task Scheduler compatibility
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

$tenantId = ''
$vaultName = ""
$secretName = ""
$applicationId = ""

# Power BI Application Authentication
$secret = Get-AzKeyVaultSecret -VaultName $vaultName -Name $secretName -AsPlainText
$securePassword = $secret | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $applicationId, $securePassword
Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credential -TenantId $tenantId

# Get PAT (through OAuth2 client credentials flow)
$clientId = $applicationId
$clientSecret = $secret
$scope = "2ff814a6-3304-4ab8-85cb-cd0e6f879c1d/.default"

$body = @{
    client_id     = $clientId
    scope         = $scope
    client_secret = $clientSecret
    grant_type    = "client_credentials"
}

$response = Invoke-RestMethod -Method Post `
    -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" `
    -ContentType "application/x-www-form-urlencoded" `
    -Body $body

$accessToken = $response.access_token

# ---------------- MAIN SCRIPT ----------------

$datasetId = ""
$workspaceId = ""

$datasources = Get-PowerBIDatasource -DatasetId $datasetId -WorkspaceId $workspaceId
$datasource = $datasources[0]

# # if no gateway, stop
if (-not $datasource.GatewayId -or -not $datasource.DatasourceId) {
    Write-Host "No datasource found or no gateway associated with the datasource. Exiting script."
    return
}

$gatewayId = $datasource.GatewayId
$datasourceId = $datasource.DatasourceId

# Take Over
Invoke-PowerBIRestMethod -Url ("https://api.powerbi.com/v1.0/myorg/groups/$workspaceId/datasets/$datasetId/Default.TakeOver") -Method Post -ErrorAction Stop

# Bind to Gateway (because after taking over, the dataset is not bound to the same gateway)
$body = @{
    "gatewayObjectId"     = $gatewayId
    "datasourceObjectIds" = @($datasourceId)
} | ConvertTo-Json -Depth 10


Invoke-PowerBIRestMethod -Url ("https://api.powerbi.com/v1.0/myorg/groups/$workspaceId/datasets/$datasetId/Default.BindToGateway") `
    -Method Post `
    -Body $body `
    -ContentType "application/json" `
    -ErrorAction Stop

# Reget the datasource after taking over and binding to gateway (this is necessary because the datasource might change after these operations)
$datasources = Get-PowerBIDatasource -DatasetId $datasetId -WorkspaceId $workspaceId
$datasource = $datasources[0]

$datasourceId = $datasource.DatasourceId
$gatewayId = $datasource.GatewayId

$body = @{
    credentialDetails = @{
        credentials                 = "{""credentialData"": [{""name"": ""accessToken"", ""value"": ""$accessToken""}]}"
        credentialType              = "OAuth2"
        encryptedConnection         = "Encrypted"
        encryptionAlgorithm         = "None"
        privacyLevel                = "Public"
        useEndUserOAuth2Credentials = $false
        useCallerAADIdentity        = $false
    }
} |  ConvertTo-Json -Depth 10

# Update the datasource with the new credentials
Invoke-PowerBIRestMethod -Url ("https://api.powerbi.com/v1.0/myorg/gateways/$($gatewayId)/datasources/$($datasourceId)") -Method PATCH -Body $body -ErrorAction Stop -ContentType "application/json"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jun 2025 12:39:16 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Service-Principal-Credential-Configuration-Error/m-p/4746543#M62027</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-06-27T12:39:16Z</dc:date>
    </item>
  </channel>
</rss>

