Hello, I’m writing some nodejs code on my server which needs to call the Particle REST Api.
I created an OAuth client (two legged authentication) and I pass the client_id and the client_secret to loginAsClientOwner function of the Particle js SDK.
Unluckily I cannot authenticate, and I get the following error:
Reading again all the docs I found this sentence (in the tutorial):
"2. Add OAuth Credentials to SDK
For both the mobile & JavaScript SDKs, you will need to add your client credentials to a configuration file. The client application will need the client credentials that you just generated "
So, I guess I don’t need to send the cliend ID and secret as params when calling the login function, but I need to save them somewhere in a configuration file of the SDK.
Ok, I solved this.
For others of you who will face the same problem, here’s how to do it (quite easy indeed):
The defaults are saved in internal clientId and clientSecret variables in Particle constructor (Particle.js):
constructor(options = {}) {
// todo - this seems a bit dangerous - would be better to put all options/context in a contained object
Object.assign(this, Defaults, options);
this.context = {};
this.agent = new Agent(this.baseUrl);
}
So, that’s just a matter of changing them:
// I get the credentials saved in a json configuration file
const clientId = configSettings.get('particle.client_id');
const clientSecret = configSettings.get('particle.client_secret');
// I save the credential in the Particle object to overwrite the default values
particle.clientId = clientId;
particle.clientSecret = clientSecret;
// And not I can connect
particle.loginAsClientOwner({})
.then((data) => {
token = data.body.access_token;
connected = true;
logger.logStartupMessage("Connected to Particle Cloud with ClientID/Secret")
})
.catch((error) => {
connected = false;
logger.logError(error);
});
I tried to follow you code example as good as I could. But it does not seem to work for me.
const functions = require('firebase-functions')
const Particle = require('particle-api-js')
const configSettings = require('./configSettings.json')
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.helloWorld = functions.https.onRequest((request, response) => {
// Start a particle instance
const particle = new Particle()
// Setup the client access
particle.clientId = configSettings.clientId
particle.clientSecret = configSettings.clientSecret
// Start the client to get an access token
particle.loginAsClientOwner({}).then(
function(data) {
// Grab the token
const token = data.body.access_token
// Call the print function that should be exposed by `Particle.function("print", methodThatPrints)`
particle.callFunction({
deviceId: configSettings.deviceId,
name: 'print',
argument: '"Hello **fire**base"',
auth: token,
})
},
function(err) {
console.log('API call completed on promise fail: ', err)
}
)
response.send('Printed..')
})
But it throws this error at me
Error: HTTP error 400 from https://api.particle.io/v1/devices/[DEVICEID]/print - Permission denied at /srv/node_modules/particle-api-js/lib/Agent.js:204:19 at Request.callback (/srv/node_modules/superagent/lib/node/index.js:728:3) at parser (/srv/node_modules/superagent/lib/node/index.js:916:18) at IncomingMessage.res.on (/srv/node_modules/superagent/lib/node/parsers/json.js:19:7) at emitNone (events.js:111:20) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1064:12) at _combinedTickCallback (internal/process/next_tick.js:139:11) at process._tickDomainCallback (internal/process/next_tick.js:219:9)
Bonus info: I got two-step authentication enabled... That's why I try to use the Particle.loginAsClientOwner({}).