cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ivanferr92
New Member

Power BI REST API Update datasource credentials

Hi,

 

Is it possible to use the Powerbi REST API to update the login credentials to a dataset?

I found something similar for gateway-managed dataset: https://docs.microsoft.com/en-us/rest/api/power-bi/gateways/adddatasourceuser#example but all my dataset are on Azure Cloud (Azure SQL) so I can't use gateway.

 

Thank you! 

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

HI @ivanferr92,

I also check the rest API documents but not found any apis to direct operate the data source credentials. (some of API can update the connection strings but they not mention/contains any parameters to operate on data credentials)
I guess power bi service may not want to share them due to security reasons.

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

5 REPLIES 5
v-shex-msft
Community Support
Community Support

HI @ivanferr92,

I also check the rest API documents but not found any apis to direct operate the data source credentials. (some of API can update the connection strings but they not mention/contains any parameters to operate on data credentials)
I guess power bi service may not want to share them due to security reasons.

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

Are you looking for Power BI .NET SDK v3 code to update datasource credentials. Here is a code snippet.

 

public static void PatchSqlDatasourceCredentials(Guid WorkspaceId, string DatasetId, string UserName, string UserPassword) {

      PowerBIClient pbiClient = TokenManager.GetPowerBiClient(requiredScopes);
      var datasources = (pbiClient.Datasets.GetDatasourcesInGroup(WorkspaceId, DatasetId)).Value;
      // find the target SQL datasource
      foreach (var datasource in datasources) {
        if (datasource.DatasourceType.ToLower() == "sql") {
          // get the datasourceId and the gatewayId
          var datasourceId = datasource.DatasourceId;
          var gatewayId = datasource.GatewayId;
          // Create UpdateDatasourceRequest to update Azure SQL datasource credentials
          UpdateDatasourceRequest req = new UpdateDatasourceRequest {
            CredentialDetails = new CredentialDetails(
              new BasicCredentials(UserName, UserPassword),
              PrivacyLevel.None,
              EncryptedConnection.NotEncrypted)
          };
          // Execute Patch command to update Azure SQL datasource credentials
          pbiClient.Gateways.UpdateDatasource((Guid)gatewayId, (Guid)datasourceId, req);
        }
      };

    }

 

Source code can be found here: https://github.com/TedPattison/DatasetManagement/blob/master/DatasetManagement/Models/PowerBiManager...

above code throw "Operation returned an invalid status code 'Unauthorized'  on "client.Gateways.UpdateDatasource"   event thou I have given all powerBI API permission include Tenant.ReadWrite.   I don't use the gateway, Is this the reason for the error?

There is still a gateway ID and you need to use the Gateway API even when there is no actual Gateway. 

 

Are you using service principal? If so, did you go through all the proper configuration to enable service principal access to Power BI API in your tenant and add servide principal as gatewway admin?

It's still not clear to me how this works as for Azure datasources, the docs say that the credentials are "per user" and so even if setting the credentals by an SP, wouldn't each user then have to enter the credentials themselves anyway?

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are You Ready?

Test your skills now with the Cloud Skills Challenge.

Top Solution Authors