Need Working Example of Nest REST API without using Firebase API

一世执手 提交于 2019-12-21 17:56:13

问题


I'm struggling trying to find a working example of writing data to the Nest Thermostat API using plain rest. Attempting to write a C# app and cannot use Firebase. The multiple Curl examples posted so far do not work. I have a valid auth_token and can read data without issues. Finding the correct post url is elusive. Can anyone assist?

Examples like

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

don't change any data.


回答1:


Two things. First, follow redirects with -L. Second, put directly to the away data location, like

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

The PUT overwrites all data at a location. The previous command would logically be setting the structure's data to just {"away":"away"}.




回答2:


user3791884, Any luck with your C# PUT? Here is C# code that works:

    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 writes this to the Output window: "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 }"

These two methods return a valid structure of my data.

1/ command line 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;
}


来源:https://stackoverflow.com/questions/24499826/need-working-example-of-nest-rest-api-without-using-firebase-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!