So I'm doing a Get call that gives me a link to make another get call later on. They both work perfectly fine on Postman, but when I try to use RestSharp, I am getting a 401 unauthorized on the second call. I've tried using HTTPClient and HTTPWebRequest but I am getting the same results.
Here's what my code looks like with RestSharp
string token = "tokenforbothcalls";
var request = new RestRequest("first/api", Method.Get);
var certfolder = @"C:\folderpath";
var cerFile = Path.Combine(certFolder, "cert.pfx");
var certificate = new X509Certificate2(certFile, "pw", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
var clientOptions = new RestClientOptions("https://baseurl.com");
clientOptions.ClientCertificates = new X509CertificateCollection();
clientOptions.ClientCertificates.Add(certificate);
var client = new RestClient(clientOptions);
client.AddDefaultHeader("Authorization", string.Format("Bearer {0}", token));
request.AddHeader("Prefer", "respond-async");
var response = await client.ExecuteAsync(request);
So that works perfectly fine, but when I get to the second call, I keep getting a 401.
var headers = response.Headers.ToList();
//some more logic to get the link from the first call.
clientOptions = new RestClientOptions("https://baseurl.com");
clientOptions.ClientCertificates = new X509CertificateCollection();
clientOptions.ClientCertificates.Add(certificate);
client = new RestClient(clientOptions);
client.AddDefaultHeader("Authorization", string.Format("Bearer {0}", token));
request = new RestRequest("new/api", Method.Get);
response = await client.ExecuteAsync(request);