I encountered a similar problem in my implementation. I've explored various solutions, but none of them have been effective so far. Eventually, I discovered that Postman handles HTTP redirection automatically. However, when using RestSharp for implementation, the headers we initially set are not included in the redirected request.
To verify, disable the "Automatically follow redirects" setting in Postman. After turning off this setting, if the issue still persists in Postman, it indicates that the problem lies in the extraction process.

So I added a check that resends the API request of receiving a 401 with the below code.
... rest of your code
RestResponse response = await client.ExecuteAsync(request);
int numericStatusCode = (int)response.StatusCode;
if (numericStatusCode == 401)
{
var redirectedClient = new RestClient(response.ResponseUri.ToString());
var newResponse = redirectedClient.Execute(request);
Console.WriteLine(newResponse.ResponseStatus);
}
Here is my full code:
var url = $"{_apiCredentials.DataServer}/test/data/contracts/";
var options = new RestClientOptions(url)
{
MaxTimeout = -1,
Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(
accessToken.Token, // Update the token
"Bearer")
};
var client = new RestClient(options);
var request = new RestRequest("", Method.Get);
RestResponse response = await client.ExecuteAsync(request);
int numericStatusCode = (int)response.StatusCode;
if (numericStatusCode == 401)
{
var redirectedClient = new
RestClient(response.ResponseUri.ToString());
var newResponse = redirectedClient.Execute(request);
Console.WriteLine(newResponse.ResponseStatus);
}