Forum Discussion

ivanferr92's avatar
ivanferr92
New Member
6 years ago
Solved

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! 

  • Anonymous's avatar
    Anonymous
    6 years ago

    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

11 Replies

  • If it helps anyone still writing this by hand: the payload has to be encrypted the way Microsoft's SDK does it.

    • 1024-bit gateway key: RSA-OAEP (SHA-1), in 85-byte pieces
    • 2048-bit key: AES-256-CBC with HMAC-SHA256, both keys wrapped with RSA-OAEP (SHA-256)

    There is a PowerShell port of it in GatewayLens, a free, open-source console for gateway admins that I wrote - see the CREDENTIAL ENCRYPTION section of GatewayLens.ps1: GatewayLens on GitHub

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    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

    • TedPattison's avatar
      TedPattison
      Icon for Microsoft Employee rankMicrosoft Employee

      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.cs

      • BartHuls's avatar
        BartHuls
        Icon for Helper I rankHelper I

        The above code throws me a BadRequest.
        Has someone this working, so set the DataSource per model  (and not set the shared one)