我正在尝试从Nest API获取我的设备/结构数据的JSON响应。 “ https://developer-api.nest.com/ ”
我已经能够使用Postman使用相同的URL检索数据,同时仅指定内容类型和授权标头。 我基本上将Postman的代码片段复制到我的程序中,但是在运行时我收到401错误。
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);这是响应内容:
{"error":"unauthorized","type":"https://developer.nest.com/documentation/cloud/error-messages#auth-error","message":"unauthorized","instance":"e0a35869-6726-4192-85d5-d31482b28269"}我是否需要以某种方式对access_token进行编码?
谢谢你的回复。
以下是输出结果的示例:
{ "devices": { "thermostats": { "fbk1Kr983gWp-eN-V9pxnOcTsTatUX5X": { "humidity": 15, "locale": "en-US", "temperature_scale": "F", ...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", ...最满意答案
发现了这个问题。
Nest API的初始响应(大部分时间)是重定向到其Firebase API服务器。 我通过添加下面的if语句来处理重定向:
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); }请注意,我将FollowRedirects属性设置为false。 第二个响应内容包含预期的JSON数据。
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.
更多推荐
发布评论