Help with REST GET in C# with HttpClient


#1

I’m having some trouble making RESTful API calls in my c# dotnet core API. I’ve played with the REST services via Postman with success. However, once I try to make those calls in C#, I am getting 401 Unauthorized errors.

Effectively my code is as follow:

var httpClient = new HttpClient();
 var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"https://developer-api.nest.com/devices/thermostats/{ConfigurationManager.NestThermostatId}/{temperatureLabel}");
        requestMessage.Headers.Add("Authorization", $"Bearer {ConfigurationManager.NestAuthToken}");

 var response = await httpClient.SendAsync(requestMessage);

Where the token in the header is the same token I am using in Postman, which works just fine. Here is the curl from Postman (edited):

curl -X GET \
https://developer-api.nest.com/devices/thermostats/{thermostatid}/ambient_temperature_f \
-H 'Authorization: Bearer {token}' \
-H 'Postman-Token: 3cc3beb6-fef2-4bd3-bfad-08908122d7ac' \
-H 'cache-control: no-cache'

Someone please help me figure out what I am doing wrong. Thank you!


#2

Nevermind! I figured out that the request gets redirected and the HttpClient library doesn’t follow the redirect, for security purposes. I’ve resolved this through some additional code. Mods please delete.