Need a working NEST REST API example without using the Firebase API

I'm struggling to find a working example of writing data in the Nest Thermostat API, using a simple rest. Trying to write a C # application and not use Firebase. A few Curl examples published so far do not work. I have a valid auth_token and you can read the data without problems. Finding the right URL is elusive. Can anyone help?

Examples for example

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

do not change any data.

+4
source share
2 answers

Two things. Redirect first with -L. Secondly, direct location at the data location, for example

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

+4

user3791884, # PUT? #, :

    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, : { Access-Control-Allow-Origin: * Cache-Control: no-cache, max-age = 0, private -: 15 Content-Type: application/json; = UTF-8 } "

.

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

2/#

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

Source: https://habr.com/ru/post/1546628/


All Articles