Start a new topic
Answered

nodejs request "gets acces denied"

i do this in nodejs, not working? (the curl works)

i shortened the tokens:)

this is what i get back?


403 { error: { message: 'Forbidden', trace: [ 'Permissions denied' ] } }


var token = "MDQ2ZjNlY2NmNDhkOTc2YWI4MGQ2YzFkMzIzYWI3MDk1NzI3MWQzMGFjYjVkNjdhYzI";

var postData = JSON.stringify({

  "frames": [

        {

            "index": 0,

            "text": "mBwired",

            "icon": "i291"

        }

    ]

});

console.log(postData);

//Load the request module

var request = require('request');

//Lets configure and request

request({

    url: 'https://developer.lametric.com/api/V1/dev/widget/update/com.lametric.6f3fdd9d/1', 

    method: 'POST',

    headers: {

  'x-Accesstoken': token,

  'Cache-Control': 'no-cache'

    },

    json: postData

}, function(error, response, body){

    if(error) {

        console.log(error);

    } else {

        console.log(response.statusCode, body);

    }

});


Best Answer

For one, you probably shouldn't include your token and app ID (the "com.lametric.XXXXXXX" bit of the URL) or else anyone will be able to push data to your app!


Other than that though, you should probably use 

body: postData

rather than 

json: postData

Despite the fact that the app takes the data in JSON format, it is actually looking for a stringified JSON object. Since you've already crunched your post data through JSON.stringify(), that should be exactly what it's looking for. Taking a peek at some stuff on SO (since I don't know node.js) it looks like the "json" key is to specify whether the body is JSON, not to actually specify the body text itself (and this is handled elsewhere for LaMetric).


I also noticed you're missing a header that they indicated in their sample code, and have another written wrong:  

'Accept': 'application/json',
'X-Access-Token': token

Accept is absent from your headers, and you have "x-Accesstoken" instead of "X-Access-Token" (this should be in addition to the Cache-Control header). Try adding that and see if it helps. It might actually be denying your request because the X-Access-Token header wasn't specified correctly (makes sense for a 403 error).


Answer

For one, you probably shouldn't include your token and app ID (the "com.lametric.XXXXXXX" bit of the URL) or else anyone will be able to push data to your app!


Other than that though, you should probably use 

body: postData

rather than 

json: postData

Despite the fact that the app takes the data in JSON format, it is actually looking for a stringified JSON object. Since you've already crunched your post data through JSON.stringify(), that should be exactly what it's looking for. Taking a peek at some stuff on SO (since I don't know node.js) it looks like the "json" key is to specify whether the body is JSON, not to actually specify the body text itself (and this is handled elsewhere for LaMetric).


I also noticed you're missing a header that they indicated in their sample code, and have another written wrong:  

'Accept': 'application/json',
'X-Access-Token': token

Accept is absent from your headers, and you have "x-Accesstoken" instead of "X-Access-Token" (this should be in addition to the Cache-Control header). Try adding that and see if it helps. It might actually be denying your request because the X-Access-Token header wasn't specified correctly (makes sense for a 403 error).


1 person likes this

Hi Jesse,

Thanks this works, the X-Access-Token instead of x-Accesstoken did the trick.

body:postData I did try but was not working because of the token :-)


This is now a fully working nodejs call, and yes the ID's are not real :-)



var token = "MDQ2ZjNlY2NmNDhkOTc2YWI4MGQ2YzFkMzIzYWI3QzMGFjYjVkNjdhYzI1NGMzMzk0YTFmZjA5MA==";

var postData = JSON.stringify({

  "frames": [

        {

            "index": 0,

            "text": "Hello this is Bwired.nl",

            "icon": "i291"

        }

    ]

});

console.log(postData);

//Load the request module

var request = require('request');

//Lets configure and request

request({

    url: 'https://developer.lametric.com/api/V1/dev/widget/update/com.lametric.6f3fdd9c2ff005cbb6f35f/1',

    method: 'POST',

    headers: {

  'Accept': 'application/json',

  'X-Access-Token': token,

  'Cache-Control': 'no-cache'

    },

    body: postData

}, function(error, response, body){

    if(error) {

        console.log(error);

    } else {

        console.log(response.statusCode, body);

    }

});

Anyone else think that 'pushing' messages to LaMetric is a little slow to appear?

Pushing seems to work reasonably quickly for me. My script pushes every 15 seconds, and my LaMetric updates within ~10 seconds of a push. It seems to only process the updates between "cycles", so if my app is in the middle of a frame cycle (e.g. it's showing frame 1 of 3 when the update pushes) it won't show the update until it finishes the cycle and reloads.

I may need to check that then on my side then - it could have been on a different frame when I pushed an update to the unit... only for it to actually show the update once it cycled round to the first frame.  Good spot.  When I get a minute, I'll try with just the one frame and see what happens...


Thanks!  

yes even faster for me within 5 secs i hear the ping

but stuff you can do is limited still

Login or Signup to post a comment