使用RestSharp c#Nest API 401响应错误(Nest API 401 Response Error using RestSharp c#)

编程入门 行业动态 更新时间:2024-10-06 18:21:31
使用RestSharp c#Nest API 401响应错误(Nest API 401 Response Error using RestSharp c#)

我正在尝试从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.

更多推荐

本文发布于:2023-07-17 10:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   API   RestSharp   Nest   Error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!