CreateCustomer issue

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);
                }
            );
        }

Your advice will be appreciated.

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.

Thanks @rickkas7 for your response.
I tried the following:

  • Added the client id and secret to the Particle constructor:
var particle = new Particle({client_id:'xxxxxx', client_secret:'yyyyyy'});
  • changed the function call to properly match the API:
    image
function createCustomer(){
            let customerEmail = $('#customerEMail').val();
            let customerPassword = $('#customerPassWord').val();
            $('#statusSpan').text('Send create customer request.');
            particle.createCustomer({email: customerEmail, password: customerPassword, product: productId, grant_type: 'password'}).then(
                function (data) {
                    $('#statusSpan').text('Customer created.');
                },
                function (err) {
                    $('#statusSpan').text('Error creating customer:' + err);
                }
            );
        }

Unfortunately I still get:

HTTP error 400 from https://api.particle.io/v1/products/zzzzzz/customers - Permission denied

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',
            });
        }

Your insight will be much appreciated.

I would do it the way you did it, using ajax, if that’s working.

It looks like particle-api-js uses a different grant_type, which does seem wrong to me.

Thanks for the prompt feedback!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.