I played wrestling with this topic myself. After spending much time looking through the documentation I realized that the PIN is needed to get the Authorization Code. This is a separate request. You only need to issue it once for each pin. (Indeed, it seems that you can only make the call once so save the output from the call.)
You can do this either in a terminal using CURL (as it is in the documentation) or through a REST call to the authentication server. I have provided a javascript version of this that I have used successfully in a Google Script:
/**
* This only works once for each PIN generation. Subsequent calls will fail.
* @param {String} user_pin the pin generated from the website
*/
function getNESTAuthorization(user_pin) {
var client_id = 'put your client id here';
var client_secret = 'put your client secret here';
// var user_pin = 'this is the pin you got from setting up the connection'
var theURL =
'https://api.home.nest.com/oauth2/access_token?code=' + user_pin +
'&client_id=' + client_id +
'&client_secret=' + client_secret +
'&grant_type=authorization_code';
var options = {
"method": "post",
"payload": ""
};
var resp = UrlFetchApp.fetch(theURL, options); // return is a JSON string
eval('var answer = ' + resp.getContentText());
Logger.log(answer.access_token); // This is the unbelievably long authentication token
// you need this for subsequent calls.
return answer.access_token;
}
There is probably some cool way to do this using the OAuthConfig structure but I haven't worked that one out yet.