I'm trying to get the JSON response for my devices/structures data from the Nest API. "https://developer-api.nest.com/"
I've been able to retrieve the data using Postman with the same url while only specifying the content-type and authorization headers. I essentially copied the code snippet from Postman into my program, but I'm receiving a 401 error upon running.
var client = new RestClient("https://developer-api.nest.com/");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "Bearer " + access_token);
request.AddHeader("content-type", "application/json");
IRestResponse response = client.Execute(request);
Here's the response content:
{"error":"unauthorized","type":"https://developer.nest.com/documentation/cloud/error-messages#auth-error","message":"unauthorized","instance":"e0a35869-6726-4192-85d5-d31482b28269"}
Do I need to encode the access_token in some way?
Thanks for your responses.
Here's a sample of what the output should look like:
{
"devices": {
"thermostats": {
"fbk1Kr983gWp-eN-V9pxnOcTsTatUX5X": {
"humidity": 15,
"locale": "en-US",
"temperature_scale": "F",
...
Found the issue.
The Nest API's initial response is (most of the time) a redirect to their Firebase API server. I handled the redirect by adding the if statement below:
var client = new RestClient("https://developer-api.nest.com/");
client.FollowRedirects = false;
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "Bearer " + access_token);
request.AddHeader("content-type", "application/json");
IRestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.RedirectKeepVerb) // 307
{
client.BaseUrl = new Uri(initial_response.Headers[7].Value.ToString());
response = client.Execute(request);
}
Note that I set the FollowRedirects property to false. The second response content contains the expected JSON data.