3

I am having trouble getting access tokena for Nest.

I have the nest-api working on my node.js app, but I am having trouble getting access tokens.

It doesn't matter if I connect from different clients, devices or even accounts.

I used to have php code that automated the oAuth 2.0 process of getting the token and it was working fine, but it stopped working.

It doesn't matter if I put the "OAuth Redirect URL" to get the token automatically or simply try to get it without the URL, I always get this error:

An error occurred. An error occurred while fetching client info

and that's all, no logs entries or anything, just that non-descriptive message.

For example if I create a new account and then I add a client to this account, I get a URL where I can retrieve an access token for the user account that wants to control its nest products from my client -- the URL looks like this:

https://home.testc.nestlabs.com/login/oauth2?client_id=XXXXX&state=STATE

When you enter this URL it should ask for your Nest credentials and then generate an access token. This was working fine doing the exact same steps, but now it doesn't even ask for the credentials, all I get is that message and nothing else.

John Hascall
  • 9,176
  • 6
  • 48
  • 72
  • 1
    The docs say to point to https://home.nest.com/login/oauth2?client_id=XXXXX&state=STATE – David W. Keith Aug 25 '14 at 00:41
  • It might be worth looking at this PHP class for the Nest API. I've found it very useful for accessing Nest data. https://github.com/gboudreau/nest-api – olimortimer Apr 16 '15 at 14:17

1 Answers1

0

I rigged up this batch file to assist with quickly testing Nest API read/writes and OAuth negotiations. Sorry it's kind of messy but hope it helps. You'll need to download the curl package and point to it. Also on pastebin. Save as .bat and comment/uncomment the rem lines as necessary.

@echo off

set curl_path=c:\nest\curl
set client_pin=[you get this from the provided URL on the client page]
set client_id=[you get this from the client page]
set client_secret=[you get this from the client page]
set access_token=[you'll get this from a successful oauth]

rem set new_url=https://firebase-apiserver02-tah01-iad01.dapi.production.nest.com:9553

rem TOGGLE FOR AUTH
rem set GET_AUTH=true
rem TOGGLE FOR AUTH

rem TOGGLE FOR DEAUTH
set GET_DEAUTH=true
rem TOGGLE FOR DEAUTH

rem TOGGLE FOR GET
rem set GET_DATA=true
rem TOGGLE FOR GET

rem TOGGLE FOR SET
rem set SET_DATA=true
rem TOGGLE FOR SET

rem TOGGLE FOR STREAM
rem set STREAM_DATA=true
rem TOGGLE FOR STREAM

rem DEVICE_CONFIG
rem virtual:
set dev_id1=[manually enter example devid]
rem real:
set dev_id2=

set dev_id=%dev_id1%
rem DEVICE_CONFIG_END

rem VALUE_CONFIG
set value1=target_temperature_f
set value2=name
set value3=hvac_mode
set value4=ambient_temperature_f
set value5=target_temperature_high_f

set value_name=%value3%

set new_int_value=77
set new_string_value=heat

set new_value=%new_string_value%
rem VALUE_CONFIG_END

rem STRUCT_CONFIG
set struct1=devices
set struct2=structures
set struct3=metadata
set struct4=devices/thermostats
set struct4=devices/smoke_co_alarms
set struct4=devices/company

set struct_name=%struct2%
rem STRUCT_CONFIG_END


set url=https://developer-api.nest.com/%struct_name%.json?auth=%access_token%
set url_by_id=%new_url%/devices/thermostats/%dev_id%.json?auth=%access_token%
set url_by_value=%new_url%/devices/thermostats/%dev_id%/%value_name%.json?auth=%access_token%

set header=Content-Type: application/json

rem GET_DATA
set get_struct=curl\curl.exe -k -L %url%
set get_by_id=curl\curl.exe -k -L %url_by_id%
set get_by_value=curl\curl.exe -k -L %url_by_value%

set get_url=%get_by_id%
rem GET_DATA_END

rem OAUTH
rem set oauth=curl\curl.exe -k -i -v -X POST -d "code=%client_pin%&client_id=%client_id%&client_secret=%client_secret%&grant_type=authorization_code" https://api.home.nest.com/oauth2/access_token

set oauth=curl\curl.exe -k -i -v -X POST "https://api.home.nest.com/oauth2/access_token?code=%client_pin%&client_id=%client_id%&client_secret=%client_secret%&grant_type=authorization_code"

rem SET_DATA
rem set change_num=curl\curl.exe -k -i -L -X PUT %url_by_value% -H "Content-Type: application/json; charset=UTF-8" -d "%new_int_value%"

rem set change_text=curl\curl.exe -k -i -L -X PUT "%url_by_value%" -H "%header%" -d '"%new_string_value%"'
rem set change_text=curl\curl.exe -k -i -L -X PUT -H "Content-Type: application/json; charset=UTF-8" -d "{"%value_name%":%new_value%}" %url_by_id%

rem set change_text=curl\curl.exe -k -i -L -X PUT "%url_by_value%" -H "%header%" -d '"%new_value%"'

rem THIS WORKS:
rem set change_text=curl\curl.exe -k -L -X PUT "https://developer-api.nest.com/devices/thermostats/%dev_id%?auth=%access_token%" -H "Content-Type: application/json" -d "{\"hvac_mode\":\"heat\"}"

rem set stream=curl\curl.exe -k -i -v -L -X GET -H "Accept: application/json" -H "Accept: text/event-stream" -H "Keep-Alive: 60" -H "Connection: keep-alive" "https://firebase-apiserver02-tah01-iad01.dapi.production.nest.com:9553/?auth=[FILL IN AUTH_TOKEN]"

set deauth=curl\curl.exe -k -i -v -L -X DELETE "https://api.home.nest.com/oauth2/access_tokens/%access_token%"

set set_data=%change_text%

echo.

if defined GET_AUTH %oauth%
if defined GET_AUTH echo.

if defined GET_DEAUTH %deauth%
if defined GET_DEAUTH echo.

if defined GET_DATA echo %get_url%
if defined GET_DATA echo.
if defined GET_DATA %get_url%
if defined GET_DATA echo.

if defined SET_DATA echo %set_data%
if defined SET_DATA echo.
if defined SET_DATA %set_data%
if defined SET_DATA echo.

if defined STREAM_DATA %stream%
if defined STREAM_DATA echo.

goto end_com

- To **change** the specific structure to away. The value here is a string so be sure to include the single quotes:

        curl -v -L -X PUT "https://developer-api.nest.com/structures/<STRUCTURE ID>/away?auth=<AUTH_TOKEN>" -H "Content-Type: application/json" -d '"away"'

:end_com

goto end_com2

echo %curl_path%
echo %client_pin%
echo %client_id%
echo %client_secret%
echo %access_token%

:end_com2
Enigma
  • 1,247
  • 3
  • 20
  • 51