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

Update Datasource Credentials With OAuth2 using PowerBI Rest API on C#

Hello, 

I need to update a Datasource credential, inside a Workspace on Powerbi, inside a C# Project.

I'm using the PowerBI Rest API to do it, but when I try to update the Datasource Credentials, I get an error.

Error Body:
"... HTTP/1.1 400 Bad Request
{"error":{"code":"BadRequest","message":"Bad Request","details":[{"message":"The property 'oauthcredentials' does not exist on type 'Microsoft.PowerBI.ServiceContracts.Api.GatewayDatasource'. Make sure to only use property names that are defined by the type.","target":"datasourceDelta"}]}}"

 

My code :

var AccessToken = userAuthnResult.AccessToken;
string
 restUrlPatchCredentials = PowerBiServiceRootUrl + "gateways/" + gatewayToUpdate.gatewayId + "/" + "datasources/" + gatewayToUpdate.id; PBIDataSourceCredentials dataSourceCredentials = new PBIDataSourceCredentials {     credentialType = "OAuth2",     oauthcredentials = new PBICredentialData     {         accessToken = AccessToken     } };
string jsonDelta = JsonConvert.SerializeObject(dataSourceCredentials);

// add JSON to HttpContent object and configure content type HttpContent patchRequestBody = new StringContent(jsonDelta); patchRequestBody.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json"); var method = new HttpMethod("PATCH"); var request = new HttpRequestMessage(method, restUrlPatchCredentials); request.Content = patchRequestBody; HttpClient client = new HttpClient(); clien.DefaultRequestHeaders.Add("Accept""application/json"); clien.DefaultRequestHeaders.Add("Authorization""Bearer " + AccessToken); var result = client.SendAsync(request).Result;

I think the error is the name of the "oauthcredentials" propriety, but i can't find the correct name.
Anyone having the same issue?

 

Thanks you!

2 REPLIES 2
waddockuk
New Member

i did you resolve this issue?

 

we have a very similar problem so would love to see your fix.

 

Thanks

Any updates on this issue ?

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.