cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Gerrit Regular Visitor
Regular Visitor

Update Credentials C#

Hi,

 

following the instructions given in this post: http://community.powerbi.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to...

 

I ended up with the following snippet:

 

                    var gateway = (await m_powerBIClient.Gateways.GetGatewayByIdWithHttpMessagesAsync (datasource.GatewayId,
                        cancellationToken: cancellationToken)).Body;
                    var credentials = AsymmetricKeyEncryptionHelper.EncodeCredentials (username, password, gateway.PublicKey.Exponent, gateway.PublicKey.Modulus);
                    var response = await m_powerBIClient.Gateways.UpdateDatasourceWithHttpMessagesAsync (
                        gateway.Id,
                        datasource.Id,
                        new UpdateDatasourceRequest (
                            new CredentialDetails (
                                credentials,
                                credentialType: "Basic",
                                encryptedConnection: "Encrypted",
                                encryptionAlgorithm: "RSA-OAEP",
                                privacyLevel: "Public"
                                )
                            ),
                        cancellationToken: cancellationToken);

But this leads to an internal server error. Do you know what is the missing part to get it running?

1 ACCEPTED SOLUTION

Accepted Solutions
Gerrit Regular Visitor
Regular Visitor

Re: Update Credentials C#

I have found out that the BadRequest was already in the GetGatewayByIdWithHttpMessagesAsync call. But nevertheless I found an easier way to handle the problem:

 

 

var request = new HttpRequestMessage (
                        new HttpMethod ("PATCH"),
                        $"https://api.powerbi.com/v1.0/myorg/gateways/{gatewayDatasource.GatewayId}/datasources/{gatewayDatasource.Id}");
                    request.Content = new StringContent (
                    $@" {{
                        'credentialType': 'Basic',
                        'basicCredentials': {{
                            'username': '{username}',
                            'password': '{password}'
                        }}
                    }}");
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse ("application/json");
                    await m_powerBIClient.Credentials.ProcessHttpRequestAsync (request, cancellationToken);

 

No nasty encryption as it is a secure connection anyway. Wondering why basic credentials are not supported by the C# API.

 

View solution in original post

3 REPLIES 3
Moderator Eric_Zhang
Moderator

Re: Update Credentials C#


@Gerrit wrote:

Hi,

 

following the instructions given in this post: http://community.powerbi.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-to...

 

I ended up with the following snippet:

 

                    var gateway = (await m_powerBIClient.Gateways.GetGatewayByIdWithHttpMessagesAsync (datasource.GatewayId,
                        cancellationToken: cancellationToken)).Body;
                    var credentials = AsymmetricKeyEncryptionHelper.EncodeCredentials (username, password, gateway.PublicKey.Exponent, gateway.PublicKey.Modulus);
                    var response = await m_powerBIClient.Gateways.UpdateDatasourceWithHttpMessagesAsync (
                        gateway.Id,
                        datasource.Id,
                        new UpdateDatasourceRequest (
                            new CredentialDetails (
                                credentials,
                                credentialType: "Basic",
                                encryptedConnection: "Encrypted",
                                encryptionAlgorithm: "RSA-OAEP",
                                privacyLevel: "Public"
                                )
                            ),
                        cancellationToken: cancellationToken);

But this leads to an internal server error. Do you know what is the missing part to get it running?


@Gerrit

The code demo in this thread indeed works in my test when updating a datasource for a specific gateway. To have better troubleshooting, please add try..catch block to get the detailed error message for your code.

try{

//your code here

}
catch (HttpOperationException ex)
               { 
                    //Bad Request
                    var content = ex.Response.Content; 
                    Console.WriteLine(content);
 }

By the way, have you debugged the code and which code line raised the error?

Gerrit Regular Visitor
Regular Visitor

Re: Update Credentials C#

I have found out that the BadRequest was already in the GetGatewayByIdWithHttpMessagesAsync call. But nevertheless I found an easier way to handle the problem:

 

 

var request = new HttpRequestMessage (
                        new HttpMethod ("PATCH"),
                        $"https://api.powerbi.com/v1.0/myorg/gateways/{gatewayDatasource.GatewayId}/datasources/{gatewayDatasource.Id}");
                    request.Content = new StringContent (
                    $@" {{
                        'credentialType': 'Basic',
                        'basicCredentials': {{
                            'username': '{username}',
                            'password': '{password}'
                        }}
                    }}");
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse ("application/json");
                    await m_powerBIClient.Credentials.ProcessHttpRequestAsync (request, cancellationToken);

 

No nasty encryption as it is a secure connection anyway. Wondering why basic credentials are not supported by the C# API.

 

View solution in original post

Highlighted
nicksav Regular Visitor
Regular Visitor

Re: Update Credentials C#

There is currently some issue with V2 client.Gateways.UpdateDatasourceAsync

 

The body which is sent using that method:

{
  "credentialDetails": {
    "credentials": "{\"credentialType\":\"Basic\",\"basicCredentials\":{\"username\":\"usrnmame\",\"password\":\"password\"}}"
  }
}

where the correct one should be:

{
"credentialType":"Basic",
"basicCredentials":{"username":"usrname","password":"password"}
}

 Simple http request as pointed above will work just fineSmiley Happy

Helpful resources

Announcements
Can You Solve These Challenge

Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge.

Community News & Announcements

Community News & Announcements

Get your latest community news and announcements.

Virtual Launch Event

Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

Community Kudopalooza

Win Power BI Swag with Community Kudopalooza!

Each week, complete activities and be qualified in the drawing for cool Power BI Swag.

Top Kudoed Authors
Users Online
Currently online: 44 members 1,056 guests
Please welcome our newest community members: