Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
tbarun
Regular Visitor

How to update credentials of Dataset (based on data flow) using service principal

I am facing an issue while patching the credentials of a data set. Our application is using the service principal to upload PowerBI reports to the respective PowerBI workspace. Most of the report is using Web Api sources, so we are patching the data source credentials using the below code, and it is working as expected.

 

//Create UpdateDatasourceRequest to update datasource credentials                           
var credentials = new AnonymousCredentials();
var credentialDetails = new CredentialDetails(credentials, PrivacyLevel.None, EncryptedConnection.NotEncrypted);
UpdateDatasourceRequest req = new UpdateDatasourceRequest(credentialDetails);

//Execute Patch command to update datasource credentials
await client.Gateways.UpdateDatasourceAsync((Guid)gatewayId, (Guid)datasourceId, req, cancellationToken);

 

We recently created a Power BI report with data flow as a data source (please note that the data flow is not created by the service principal), and when we tried to refresh the dataset after publishing the report to workspace, it threw an error: "Some of the data sources have missing credentials."

 

The credentials configuration is currently displayed in Power BI as follows:

Data Source Credentials.png

After going through the following tutorial, I used the code below to set the credentials.

https://learn.microsoft.com/en-us/power-bi/developer/embedded/configure-credentials?tabs=sdk3 

 

var gateway = pbiClient.Gateways.GetGatewayById(datasource.GatewayId);
var credentialsEncryptor = new AsymmetricKeyEncryptor(gateway.publicKey);
var credentialDetails = new CredentialDetails(
        credentials,
        PrivacyLevel.Private,
        EncryptedConnection.Encrypted,
        credentialsEncryptor);

 

However, because all of the data is in the cloud (Azure), obtaining the public key from the gateway is impossible.

I also tried the following code without encrypting the details, but it did not work. I received a Bad Request error and am pasting the exact error also.

 

var delta = new UpdateDatasourceRequest
{
   CredentialDetails = new CredentialDetails
    {
      CredentialType = "OAuth2",
      Credentials = "{\"credentialData\":[{\"name\":\"accessToken\", \"value\":\""+ result.AccessToken + "\"}]}",
      EncryptedConnection = "Encrypted",
      EncryptionAlgorithm = "None",
      PrivacyLevel = "None"
    }
};

await client.Gateways.UpdateDatasourceAsync((Guid)gatewayId, (Guid)datasourceId, delta, cancellationToken);

 

{"error":{"code":"DM_GWPipeline_Gateway_InvalidConnectionCredentials","pbi.error":{"code":"DM_GWPipeline_Gateway_InvalidConnectionCredentials","parameters":{},"details":[{"code":"DM_ErrorDetailNameCode_UnderlyingErrorCode","detail":{"type":1,"value":"-2147467259"}},{"code":"DM_ErrorDetailNameCode_UnderlyingErrorMessage","detail":{"type":1,"value":"The credentials provided for the PowerBI source are invalid. (Source at PowerBI.)"}},{"code":"DM_ErrorDetailNameCode_UnderlyingHResult","detail":{"type":1,"value":"-2147467259"}},{"code":"Microsoft.Data.Mashup.CredentialError.DataSourceKind","detail":{"type":1,"value":"PowerBI"}},{"code":"Microsoft.Data.Mashup.CredentialError.DataSourceOriginKind","detail":{"type":1,"value":"Web"}},{"code":"Microsoft.Data.Mashup.CredentialError.DataSourceOriginPath","detail":{"type":1,"value":"https://api.powerbi.com/powerbi/globalservice/v201606/clusterdetails"}},{"code":"Microsoft.Data.Mashup.CredentialError.DataSourcePath","detail":{"type":1,"value":"PowerBI"}},{"code":"Microsoft.Data.Mashup.CredentialError.Reason","detail":{"type":1,"value":"AccessForbidden"}},{"code":"Microsoft.Data.Mashup.MashupSecurityException.DataSources","detail":{"type":1,"value":"[{\"kind\":\"PowerBI\",\"path\":\"PowerBI\"}]"}},{"code":"Microsoft.Data.Mashup.MashupSecurityException.Reason","detail":{"type":1,"value":"AccessForbidden"}}],"exceptionCulprit":1}}}

 

I'm missing something; any suggestions you have would be greatly appreciated.

Thanks in advance. 

 

1 REPLY 1
lbendlin
Super User
Super User

Assuming that you have a Pro license you can consider raising a Pro ticket at https://powerbi.microsoft.com/en-us/support/pro/

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Kudoed Authors