4

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

不要更改任何数据。

4

2 回答 2

4

两件事情。首先,使用 -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"}.

于 2014-06-30T23:54:55.537 回答
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 将其写入输出窗口:“http.PutAsync2=StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Access-Control-Allow-Origin: * Cache-Control: no-cache, max-age=0, private Content-Length: 15 Content-Type: application/json; charset=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;
}
于 2014-07-28T19:15:01.760 回答