You never send the claim code to the endpoint. Here’s the exact code I have running on my web server:
const PARTICLE_AUTH_URL = 'https://' + process.env.PARTICLE_AUTH + '@api.particle.io';
const PARTICLE_URL = 'https://api.particle.io';
function * particleAuthReq(ctx) {
var token = yield token.bind(ctx)();
if(token.code) {
throw new Error(token + '');
}
return request.defaults({
baseUrl: PARTICLE_URL,
headers: {
'Authorization:': 'Bearer ' + token.access_token
},
json: true
});
}
function * rename(ctx, authReq, particle, name) {
var resp = yield authReq({
uri: '/v1/devices/' + particle.id,
method: 'PUT',
body: {
name: name
}
});
if(resp.statusCode !== 200) {
ctx.log.warn({rsp: resp}, 'Faild to renam particle');
throw new Error('Failed to rename particle');
}
}
function * create() {
var particle = {id: this.data.id, userId: this.user.id};
this.log.info({particle: particle}, 'create particle');
var authReq = yield particleAuthReq(this);
var resp = yield authReq.get('/v1/devices/' + particle.id);
if(resp.statusCode !== 200) {
this.log.info({particle: particle}, 'particle not claimed - claiming');
yield authReq.post('/v1/devices', {body: {id: particle.id}});
resp = yield authReq.get('/v1/devices/' + particle.id);
}
if(resp.statusCode !== 200) {
this.log.warn({rsp: resp}, 'Failed to get device from particle.io');
return {
code: 500,
message: 'Failed to create particle'
};
}
var _particle = resp.body;
if(this.data.name) {
this.log.info({particle: particle}, 'naming particle');
yield rename(this, authReq, particle, this.data.name);
_particle.name = this.data.name;
}
particle = yield Particle.create({
userId: this.user.id,
id: _particle.id,
gatewayId: this.gateway.id,
cellular: _particle.cellular,
connected: _particle.connected,
lastHeard: _particle.last_heard,
lastIPAddress: _particle.last_ip_address,
lastIccid: _particle.last_iccid,
imei: _particle.imei,
name: _particle.name,
platfromId: _particle.platfrom_id,
productId: _particle.product_id,
status: _particle.status
});
this.log.info({particle: particle}, 'particle created');
return {particle: particles.normalize(particle)};
}
function * token() {
var query = Gateway.queryOne('userId').eq(this.user.id);
query.filter('type').eq('particle');
var gateways = yield Gateway.query('userId').eq(this.user.id).exec();
var gateway;
gateways.forEach(function(g) {
if(g.type === 'particle') {
gateway = g;
}
});
if(!gateway) {
var createUserResp = yield request.post({
url: PARTICLE_AUTH_URL + '/v1/orgs/'+ PARTICLE_ORG + '/customers',
form: {
email: this.user.email,
no_password: true
}
});
if(createUserResp.statusCode >= 300) {
this.log.error({rsp: createUserResp}, 'Faild to create user on particle');
return {
code: 500,
message: 'Failed to create particle token'
};
}
gateway = yield Gateway.create({
userId: this.user.id,
name: 'Automate Green',
type: 'particle',
status: {state: 'Active'}
});
}
this.gateway = gateway;
var tokenResp = yield request.post({
url: PARTICLE_AUTH_URL + '/oauth/token',
form: {
scope: 'customer=' + this.user.email,
grant_type: 'client_credentials'
}
});
if(tokenResp.statusCode >= 300) {
this.log.error({rsp: tokenResp}, 'Faild to get particle token');
return {
code: 500,
message: 'Failed to create particle token'
};
}
return JSON.parse(tokenResp.body);
}