Notice: We're retiring Works with Nest. See the home page for more information.
En Google, luchamos por la equidad racial de la comunidad negra. Más información

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 -X POST \
  -d "code=AUTH_CODE&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=authorization_code" \
Python 2
import requests

url = ""

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)

Python 3
import http.client

conn = http.client.HTTPSConnection("")

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 =

var qs = require("querystring");
var http = require("https");

var options = {
  "method": "POST",
  "hostname": "",
  "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) {

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

  code: 'AUTH_CODE',
  client_id: 'CLIENT_ID',
  client_secret: 'CLIENT_SECRET',
  grant_type: 'authorization_code'

package main

import (

func main() {

    url := ""

    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)

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=
        Request request = new Request.Builder()

        Response response = client.newCall(request).execute();


To use the Java code example:

1. Download the latest okhttp and okio Jar files at Additionally, download the Kotlin stdlib file at 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
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
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