From 47216d87a8a7466b1afd63eb3a7b5eeb04a49169 Mon Sep 17 00:00:00 2001 From: "jordan.yordanov" Date: Sat, 28 Dec 2024 15:06:25 -0500 Subject: [PATCH] Added UsernamePasswordAsync overload to customize the deserialization process in the cases when - during Salesforce downtime - response other than json is sometimes returned. --- .../AuthenticationClient.cs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/NetCoreForce.Client/AuthenticationClient.cs b/src/NetCoreForce.Client/AuthenticationClient.cs index 93a2c51..abcc5ce 100644 --- a/src/NetCoreForce.Client/AuthenticationClient.cs +++ b/src/NetCoreForce.Client/AuthenticationClient.cs @@ -119,6 +119,24 @@ public Task UsernamePasswordAsync(string clientId, string clientSecret, string u /// Thrown if the authentication fails public async Task UsernamePasswordAsync(string clientId, string clientSecret, string username, string password, string tokenRequestEndpointUrl) { + await UsernamePasswordAsync(clientId, clientSecret, username, password, tokenRequestEndpointUrl, + response => JsonConvert.DeserializeObject(response), + response => JsonConvert.DeserializeObject(response)); + } + + /// + /// Authenticate using the "Username and Password" auth flow + /// + /// Client ID, a.k.a. Consumer Key + /// Client Secret, a.k.a. Consumer Secret + /// Username + /// Password + /// Token request endpoint URL, e.g. https://login.salesforce.com/services/oauth2/token + /// Function to deserialize the access token response + /// Function to deserialize the auth error response + /// Thrown if the authentication fails + public async Task UsernamePasswordAsync(string clientId, string clientSecret, string username, string password, string tokenRequestEndpointUrl, Func DeserializeAccessToken, Func DeserializeAuthError) + { #if DEBUG Stopwatch sw = new Stopwatch(); sw.Start(); @@ -153,7 +171,7 @@ public async Task UsernamePasswordAsync(string clientId, string clientSecret, st if (responseMessage.IsSuccessStatusCode) { - this.AccessInfo = JsonConvert.DeserializeObject(response); + this.AccessInfo = DeserializeAccessToken(response); } else if (responseMessage.StatusCode == HttpStatusCode.NotFound) { @@ -162,7 +180,7 @@ public async Task UsernamePasswordAsync(string clientId, string clientSecret, st } else { - var errorResponse = JsonConvert.DeserializeObject(response); + var errorResponse = DeserializeAuthError(response); throw new ForceAuthException(errorResponse.Error, errorResponse.ErrorDescription, responseMessage.StatusCode); } #if DEBUG