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
shiva
Regular Visitor

Unable to update data source credentials using PowerBI API

I have created a PBIX that used direct query to SQL Azure DB. I initially created this report in development environment that points to my development DB.

 

For production purpose, I programatically upload this report to a group in Power BI. Likewise for other reports. Then I try to programatically update the data source along with credentials using PowerBI API. But it fails. It updates the data source (DB server and catalog), but not the credentials.

 

Below is my sample code that updates the data source connection string:
foreach( var ds in client.Datasets.GetDatasetsInGroup(grp.Id).Value)
client.Datasets.SetAllDatasetConnectionsInGroup(grp.Id,ds.Id , new ConnectionDetails(ConnectionString));

 

Am I doing it the right way. Kindly advice.

5 REPLIES 5
Eric_Zhang
Employee
Employee


@shiva wrote:

I have created a PBIX that used direct query to SQL Azure DB. I initially created this report in development environment that points to my development DB.

 

For production purpose, I programatically upload this report to a group in Power BI. Likewise for other reports. Then I try to programatically update the data source along with credentials using PowerBI API. But it fails. It updates the data source (DB server and catalog), but not the credentials.

 

Below is my sample code that updates the data source connection string:
foreach( var ds in client.Datasets.GetDatasetsInGroup(grp.Id).Value)
client.Datasets.SetAllDatasetConnectionsInGroup(grp.Id,ds.Id , new ConnectionDetails(ConnectionString));

 

Am I doing it the right way. Kindly advice.


@shiva

You'll have to set credentials after updating the connection string, see Set Credentials, you'll get the gatewayid from Get BoundGatewayDatasources.

Thanks for the reply. 

 

I am not using any gateway. I am using PowerBI with SQL Azure. Why should I create a gateway ? 

 


@shiva wrote:

Thanks for the reply. 

 

I am not using any gateway. I am using PowerBI with SQL Azure. Why should I create a gateway ? 

 


@shiva

Azure SQL in Power BI internally actually uses gateway, however there's nothing for you to configure. If you call  Get BoundGatewayDatasources by passing the Azure SQL datasetid, you'll get the gatewayid.

Status.png

 

Hi Eric, kindly refer to the above pic,  I am getting forbidden access error. What could be the reason? Below is the code I used:

 

string uri = string.Format("https://api.powerbi.com/v1.0/myorg/gateways/{0}/datasources/{1}", result[0].GatewayId, item.Id);

var Uri = new Uri(uri);

var content = new StringContent("{ \"credentialType\": \"Basic\", \"basicCredentials\": { \"username\": \"userName\", \"password\": \"password\" }}", System.Text.Encoding.Default, "application/json");

using (var request = new HttpRequestMessage { Method = new HttpMethod("PATCH"), RequestUri = Uri, Content = content })
{

var rep = client.HttpClient.SendAsync(request).Result;
}

 

 


@shiva wrote:

Status.png

 

Hi Eric, kindly refer to the above pic,  I am getting forbidden access error. What could be the reason? Below is the code I used:

 

string uri = string.Format("https://api.powerbi.com/v1.0/myorg/gateways/{0}/datasources/{1}", result[0].GatewayId, item.Id);

var Uri = new Uri(uri);

var content = new StringContent("{ \"credentialType\": \"Basic\", \"basicCredentials\": { \"username\": \"userName\", \"password\": \"password\" }}", System.Text.Encoding.Default, "application/json");

using (var request = new HttpRequestMessage { Method = new HttpMethod("PATCH"), RequestUri = Uri, Content = content })
{

var rep = client.HttpClient.SendAsync(request).Result;
}

 

 


@shiva

  • The registered app requirs scope: Dataset.ReadWrite.All or Dataset.Read.All, check the registered app in Azure portal.
  • Add try catch block to get the very specific error message.
    try{
    
    //your code here
    
    }
    catch (HttpOperationException ex)
                   { 
                        //Bad Request
                        var content = ex.Response.Content; 
                        Console.WriteLine(content);
     }

 

 

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.