需要使用Nest REST API的工作示例而不使用Firebase API

时间:2014-06-30 22:21:35

标签: nest-api

我正在努力寻找一个使用普通休息将数据写入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"}'

不要更改任何数据。

2 个答案:

答案 0 :(得分:4)

两件事。首先,使用-L跟随重定向。其次,直接放到离开数据位置,如

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"}

答案 1 :(得分:2)

user3791884, 你的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将此内容写入“输出”窗口: &#34; http.PutAsync2 = StatusCode:200,ReasonPhrase:&#39; OK&#39;,版本:1.1,内容:System.Net.Http.StreamContent,标题: {   Access-Control-Allow-Origin:*   Cache-Control:no-cache,max-age = 0,private   内容长度:15   Content-Type:application / json;字符集= UTF-8 }&#34;

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

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;
}