【问题标题】:Need Working Example of Nest REST API without using Firebase API需要不使用 Firebase API 的 Nest REST API 的工作示例
【发布时间】:2014-08-21 09:32:00
【问题描述】:

我正在努力寻找一个使用普通休息将数据写入 Nest Thermostat API 的工作示例。尝试编写 C# 应用程序但无法使用 Firebase。到目前为止发布的多个 Curl 示例不起作用。我有一个有效的 auth_token 并且可以毫无问题地读取数据。找到正确的帖子网址是难以捉摸的。有人可以帮忙吗?

例如

curl -v -X PUT "https://developer-api.nest.com/structures/g-9y-2xkHpBh1MGkVaqXOGJiKOB9MkoW1hhYyQk2vAunCK8a731jbg?auth=<AUTH_TOKEN>" -H "Content-Type: application/json" -d '{"away":"away"}'

不要更改任何数据。

【问题讨论】:

    标签: nest-api


    【解决方案1】:

    两件事。首先,使用 -L 跟随重定向。二、直接放到 away 数据位置,比如

    curl -v -L -X PUT "https://developer-api.nest.com/structures/g-9y-2xkHpBh1MGkVaqXOGJiKOB9MkoW1hhYyQk2vAunCK8a731jbg/away?auth=<AUTH_TOKEN>" -H "Content-Type: application/json" -d '"away"'
    

    PUT 会覆盖某个位置的所有数据。前面的命令在逻辑上会将结构的数据设置为 {"away":"away"}

    【讨论】:

    • 它看起来应该可以工作但没有做任何事情。卷曲没有错误。恒温器状态不会改变。我已经尝试了所有可写参数的各种变化。没有任何效果。
    • 如果选择了错误的范围,您应该会看到 401。这听起来像是不遵循重定向的情况,在非详细模式下 curl 不会报告。
    【解决方案2】:

    用户3791884, 你的 C# PUT 运气好吗?这是有效的 C# 代码:

        using System.Net.Http;
    
    private async void changeAway()
    {
        using (HttpClient http = new HttpClient()) 
        {
            string url = "https://developer-api.nest.com/structures/" + structure.structure_id + "/?auth=" + AccessToken;
            StringContent content = new StringContent("{\"away\":\"home\"}"); // derived from HttpContent
            HttpResponseMessage rep = await http.PutAsync(new Uri(url), content);
            if (null != rep)
            {
                Debug.WriteLine("http.PutAsync2=" + rep.ToString());
            }
        }
    }
    

    Debug.WriteLine 将其写入输出窗口: “http.PutAsync2=StatusCode:200,ReasonPhrase:'OK',版本:1.1,内容:System.Net.Http.StreamContent,标题: { 访问控制允许来源:* 缓存控制:无缓存,max-age=0,私有 内容长度:15 内容类型:应用程序/json;字符集=UTF-8 }"

    这两种方法返回我的数据的有效结构。

    1/ 命令行 curl -v -k -L -X GET "https://developer-api.nest.com/structures/Za6hCZpmt4g6mBTaaA96yuY87lzLtsucYjbxW_b_thAuJJ7oUOelKA/?auth=c.om2...AeiE"

    2/C#

    private bool getStructureInfo()
    {
        bool success = false;
        try
        {
            // Create a new HttpWebRequest Object to the devices URL.
            HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("https://developer-api.nest.com/structures/?auth=" + AccessToken);
            // Define the request access method.
            myHttpWebRequest.Method = "GET";
            myHttpWebRequest.MaximumAutomaticRedirections=3;
            myHttpWebRequest.AllowAutoRedirect=true;
            myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
    
            using(HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse())
            {
                if (null != myHttpWebResponse)
                {
                    // Store the response.
                    Stream sData = myHttpWebResponse.GetResponseStream();
                    // Pipes the stream to a higher level stream reader with the required encoding format. 
                    StreamReader readStream = new StreamReader (sData, Encoding.UTF8);
    
                    Debug.WriteLine("Response Structure stream received.");
                    string data = readStream.ReadToEnd();
                    Debug.WriteLine(data);
                    readStream.Close();
                    success = deserializeStructure(data);
                }
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine("getStructure Exception=" + ex.ToString());
        }
        return success;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-07
      • 2011-05-31
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 2018-02-04
      • 2017-08-28
      • 1970-01-01
      相关资源
      最近更新 更多