HTML request via Function node

Full disclosure, I'm not very adept at HTML, I don't even like it (nothing intuitive about it).

I'm trying to set up access to the Ecobee server to read / control thermostats. This "may" become my first attempt at creating a contrib node, who knows (cause I couldn't find one). Anyway, the first step is to ensure you have a valid token, or to refresh if your token is expired. Ecobee's API instructions say this is how you do the refresh ...

apiKey = $('#apiKey').val();
refreshToken = $('#refreshToken').val();
   
var url = "https://api.ecobee.com/token";
var data = "grant_type=refresh_token&code=".concat(refreshToken).concat("&client_id=").concat(apiKey);
      
$.post(url, data, function(resp) {
    var response = JSON.stringify(resp, null, 4);
      $('#refreshTokenResponse').html(response);
}, 'json');

So I created a Function node with the output feeding an HTTP Request Node (set to POST) as follows ....

var apiKey = Ecobee_ID.apiKey;
var authCode = Ecobee_ID.auth_code;
var refreshToken = Ecobee_ID.refresh_token;

var url = "https://api.ecobee.com/token";
var data = "grant_type=refresh_token&code=" + refreshToken + "&client_id=" + apiKey;
msg = {url: url, payload: data};
return(msg);

The response I get from the server is as follows:

payload: object
error: "invalid_request"
error_description: "The request is malformed. Check parameters."
statusCode: 400

https://www.ecobee.com/home/developer/api/introduction/index.shtml

So according to the instructions, the content you send should be JSON rather than a string and the URL you send to should include the API version number.

So try changing the data variable to:

var data = {
   "grant_type": "refresh_token",
   "code": refreshToken,
   "client_id": apiKey,
}

The http-request node should automatically send that as JSON content.

Good thinking so I tried that with the same result. The response did include

error_url: "https://tools.ietf.org/html/rfc6749#section-5.2"
and for what it's worth, describes:

5.2. Error Response
The authorization server responds with an HTTP 400 (Bad Request) status code (unless specified otherwise) and includes the following parameters with the response:
error
REQUIRED. A single ASCII [USASCII] error code from the following:

     invalid_request
           The request is missing a required parameter, includes an
           unsupported parameter value (other than grant type),
           repeats a parameter, includes multiple credentials,
           utilizes more than one mechanism for authenticating the
           client, or is otherwise malformed.
     invalid_grant
           The provided authorization grant (e.g., authorization
           code, resource owner credentials) or refresh token is
           invalid, expired, revoked, does not match the redirection
           URI used in the authorization request, or was issued to
           another client.

Sorry, as I don't use that API, I can't really help further other than to say that you should probably work through the documentation and see if you can find any other examples to give you some clues.

NP Thanks TotallyInformation for your thoughts. I thought you had it.

As I look into this deeper I believe there are some "include" parameters required. When (if) I ever figure it out I'll re-post here.

Ecobee does provide a stand alone node.js application that is accessed via port 3000. Located here Ecobee node.js app

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.