Notice: We're retiring Works with Nest. See the home page for more information.

Sample Code for Authorization

Use the code samples on this page to get an access token. You can then use the access token to make API calls to read and write to structures and devices.

Before you begin, see Authenticating with OAuth 2.0.

On your OAuth client's Overview tab:

  • Client ID is your CLIENT_ID
  • Client Secret is your CLIENT_SECRET

Before each POST call, get a new authorization code:

  1. Copy and reload your authorization URL
  2. Click [ACCEPT] and copy the new authorization code
  3. Change the value of the POST's code parameter to match the new authorization code
OAuth details

In the code samples, be sure to replace AUTH_CODE, CLIENT_ID, and CLIENT_SECRET.

Access token request examples

Use these examples to get an access token.

Curl
curl -X POST \
  -d "code=AUTH_CODE&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=authorization_code" \
  "https://api.home.nest.com/oauth2/access_token"
Python 2
import requests

url = "https://api.home.nest.com/oauth2/access_token"

payload = "code=AUTH_CODE&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=authorization_code"

headers = {'content-type': 'application/x-www-form-urlencoded'}

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)
Python 3
import http.client

conn = http.client.HTTPSConnection("api.home.nest.com")

payload = "code=AUTH_CODE&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=authorization_code"

headers = { 'content-type': "application/x-www-form-urlencoded" }

conn.request("POST", "/oauth2/access_token", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))
Node.js
var qs = require("querystring");
var http = require("https");

var options = {
  "method": "POST",
  "hostname": "api.home.nest.com",
  "port": null,
  "path": "/oauth2/access_token",
  "headers": {
    "content-type": "application/x-www-form-urlencoded"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(qs.stringify({
  code: 'AUTH_CODE',
  client_id: 'CLIENT_ID',
  client_secret: 'CLIENT_SECRET',
  grant_type: 'authorization_code'
}));

req.end();
Go
package main

import (
    "fmt"
    "strings"
    "net/http"
    "io/ioutil"
)

func main() {

    url := "https://api.home.nest.com/oauth2/access_token"

    payload := strings.NewReader("code=AUTH_CODE&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=authorization_code")

    req, _ := http.NewRequest("POST", url, payload)

    req.Header.Add("content-type", "application/x-www-form-urlencoded")

    res, _ := http.DefaultClient.Do(req)

    defer res.Body.Close()
    body, _ := ioutil.ReadAll(res.Body)

    fmt.Println(res)
    fmt.Println(string(body))
}
Java
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.MediaType;
import okhttp3.Response;

public class Sample {

    public static void main(String[] args) throws IOException {
        OkHttpClient client = new OkHttpClient();

        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body =
               RequestBody.create(mediaType, "code=AUTH_CODE&client_id=
               CLIENT_ID&client_secret=CLIENT_SECRET
               &grant_type=authorization_code");
        Request request = new Request.Builder()
          .url("https://api.home.nest.com/oauth2/access_token")
          .post(body)
          .build();

        Response response = client.newCall(request).execute();
        System.out.println(response.body().string());
    }

}

To use the Java code example:

1. Download the latest okhttp and okio Jar files at http://square.github.io/okhttp/. Additionally, download the Kotlin stdlib file at https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-stdlib if your version of okhttp depends on it.

2. Construct your code.

3. Compile and run the program with the following class-path settings.

Substitute your library version numbers in the javac and java commands below:
javac -cp .:okhttp-<ver>.jar:okio-<ver>.jar Sample.java
java -cp .:okhttp-<ver>.jar:okio-<ver>.jar:kotlin-stdlib-<ver>.jar Sample

For instance, if you have the following versions 3.11.0, 2.1.0, and 1.3.0, your commands would look like this:
javac -cp .:okhttp-3.11.0.jar:okio-2.1.0.jar Sample.java
java -cp .:okhttp-3.11.0.jar:okio-2.1.0.jar:kotlin-stdlib-1.3.0.jar Sample

Access token response

For each sample, the response includes the access token.

  "access_token": "c.twC2q...",
  "expires_in": 315360000