Skip to content

Commit 64480a1

Browse files
gautamdshethGautam Sheth
andauthored
Improved logging for fetching access token (#4981)
* Improved logging for fetching access token * Update TokenHandler.cs --------- Co-authored-by: Gautam Sheth <gautam.sheth@staffbase.com>
1 parent 0a56f0e commit 64480a1

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/Commands/Base/TokenHandler.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ private static async Task<string> GetAccessTokenWithFederatedTokenAsync(string c
377377
{
378378
try
379379
{
380-
Framework.Diagnostics.Log.Debug("TokenHandler", "Retrieving Entra ID access Token with federated token...");
380+
Framework.Diagnostics.Log.Debug("TokenHandler", "Retrieving Entra ID access token with federated token...");
381381
var httpClient = Framework.Http.PnPHttpClient.Instance.GetHttpClient();
382382

383383
var queryParams = new List<string>
@@ -398,16 +398,20 @@ private static async Task<string> GetAccessTokenWithFederatedTokenAsync(string c
398398
request.Headers.Add("x-anonymous", "true");
399399

400400
var response = await httpClient.SendAsync(request);
401-
response.EnsureSuccessStatusCode();
402401

403402
var responseContent = await response.Content.ReadAsStringAsync();
403+
if (!response.IsSuccessStatusCode)
404+
{
405+
throw new HttpRequestException($"Failed to retrieve federated access token: HTTP Error {response.StatusCode}: {responseContent}");
406+
}
404407
var tokenResponse = JsonSerializer.Deserialize<Dictionary<string, object>>(responseContent);
405408

406409
return tokenResponse["access_token"].ToString();
407410
}
408411
catch (Exception ex)
409412
{
410-
throw new PSInvalidOperationException($"Failed to retrieve access token with federated token: {ex.Message}", ex);
413+
Framework.Diagnostics.Log.Error("TokenHandler", ex.Message);
414+
throw new PSInvalidOperationException(ex.Message, ex);
411415
}
412416
}
413417
}

0 commit comments

Comments
 (0)