android-NestDK stuck with pincode

◇◆丶佛笑我妖孽 提交于 2019-12-11 02:51:43

问题


I'm trying to figure out how to connect my NestDK app to the nest API. After Login in with my user and password, I am getting a "Use this Pincode to connect with Nest. XXXXXX". What am I supposed to do with this ? I expected the access_token request to be happening in the background but I get stuck on this page.

I set REDIRECT_URL to https://api.home.nest.com/oauth2/access_token?client_id=[myClientId]&code=AUTHORIZATION_CODE&client_secret=[myClientSecret]&grant_type=authorization_code

Any idea ?

Thanks


回答1:


I finaly found my mistake.

The REDIRECT_URL is to be set in the client registration page on the nest developer website and NOT ONLY in Constants.java.

I put http://localhost/ on both location and it worked immediatly.




回答2:


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.



来源:https://stackoverflow.com/questions/27390068/android-nestdk-stuck-with-pincode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!