I am trying to create a customer from a java script that I think matches the SDK’s required API, but get Error: HTTP error 400 from https://api.particle.io/v1/products/xxxxx/customers - Permission denied.
Note that I created a product and then created an oauth client related to that product. This client id and secret is what I use when calling particle.createCustomer.
function createCustomer(){
let customerEmail = $('#customerEMail').val();
let customerPassword = $('#customerPassWord').val();
$('#statusSpan').text('Send create customer request.');
particle.createCustomer({ product: 'xxxxx', client_id: 'yyyyy',
client_secret: 'zzzzz',
email: customerEmail, password: customerPassword}).then(
function (data) {
$('#statusSpan').text('Customer created.');
},
function (err) {
$('#statusSpan').text('Error creating customer:' + err);
}
);
}
The particle-api-js doesn’t appear to take the client_id and client_secret as parameters to createCustomer.
It appears that you need to pass the clientId and clientSecret as an object passed to the Particle class constructor when you calls new Particle() instead.
However, the following does work (rejoicing!), which is probably a solution, but does not explain (to me at least) why the Particle JS API call does not work.
function createCustomer(){
let customerEmail = $('#customerEMail').val();
let customerPassword = $('#customerPassWord').val();
$('#statusSpan').text('Send create customer request.');
$.ajax({
data: {
'client_id': particle.client_id,
'client_secret': particle.client_secret,
'grant_type' : 'password',
'email': customerEmail,
'password': customerPassword,
},
error: function (error) {
$('#statusSpan').text('error:'+JSON.stringify(error));
},
method: 'POST',
success: function (data) {
//ajaxResponse = JSON.parse(data).access_token;
$('#statusSpan').text('Customer created with access token:' + JSON.stringify(data));
},
url: 'https://api.particle.io/v1/products/' + productId +'/customers',
});
}