Permission denied Error from cloud API

Hi,

for a research project, we are running a Node.js server that controls >20 Particle Cores from 4 different user accounts. We control these cores using the Spark JS API. For every call, we login using the access-token of the user account, and then send a message to the core.

However, when sending messages from the node server to the different accounts, we get random “permission denied” messages.This happens especially when rapidly sending messages to different accounts at almost the same time. But is does not always happen for the same account, it seems a bit random?

Any suggestions to what could cause this, or how to debug this?

Personally, if working with different accounts, I think it might be easier to log in once for each, get the access tokens, and use that instead. That way, you won’t have to log in every time you want to switch, which delays the process.
Since Node.js is asynchronous, it might be that it’ll try to send the message to a different device, before it’s actually logged in to the appropriate account. This, of course, depends on how you coded it.
As far as debugging goes, I often find it useful to put in a lot of console.log()s, which will allow me to track where the code is actually at.
Disclaimer: I’m not that adapt at node.js, and this is just a random idea. There are likely to be way more efficient ways of doing this. For my simple projects, they sufficed.

1 Like

From my understanding, a user can own many devices, with each device having its own device ID and the access token. I don’t know what is the limit on how many cores one user account can own.

From the host side (node.js), you don’t need to have logged into control spark devices. You only need the device id and the access_token.

For example, if you try the following on a browser

https://api.spark.io/v1/devices/DEVICEID/VARNAME?access_token=ACCESSTOKEN

where DEVICEID is replaced with the core ID, ACCESSTOKEN with the core’s access_token and VARNAME is the name of the variable exposed by Spark.variable, you should get the JSON response

Your node.js based server that talks to the Spark cores should know their device ids and access tokens to perform any action based on exposed variables and functions. If the device ids and access tokens are mixed up, you would get a “invalid access token” message. If the access token is missing from the request, you would get a permission denied message

Hope this helps

1 Like

He’s using the Javascript library, where you can log in to your account, and more easily controll your devices, without having to worry about implementing POST and GET and whatnot. You then no longer need the accesstoken since the library takes care of that. Instead of having to make a POST request, you can then simply do device[i].callFunction(), amongst others.

1 Like

@Moors7. Thanks. I had used POST and GET using libcurl from C/C++ on the host, where I had to used the device id and the access token. My response was based on that. Thanks for the clarification.

2 Likes

Hi,

thanks for the comments. The code is a bit to complex to use console.log() unfortunately, but I changed the code from the Spark JS library to plain POST request, and this actually fixed the problem.

Thanks!

1 Like

As a professional NodeJS developer, I use console.log() for debugging all the time (as a matter of fact, that’s what I’m doing right now) and I promise you, our app is more complex. :smile:

1 Like

Well, that may be, and I do use the console with colors while writing modules, but I found node-inspector much easier to step through the entire code…

Could you perhaps create multiple Spark instances, one assigned to each user/access token? I too am not a big time NodeJS dev but the following makes sense to me (perhaps @tjp could weigh in here):

var spark1 = require('spark');
spark1.login({accessToken: 'ACCESS_TOKEN_1'});
var spark2 = require('spark');
spark2.login({accessToken: 'ACCESS_TOKEN_2'});
var spark3 = require('spark');
spark3.login({accessToken: 'ACCESS_TOKEN_3'});
var spark4 = require('spark');
spark4.login({accessToken: 'ACCESS_TOKEN_4'});
2 Likes