1

I am trying to post data to Woocommerce API using AngularJS. I am using the OAuth library to generate signature (I am able to GET the data successfully).

angular.module('starter.services',[])
.factory('AuthService', function($http){

    function getData(urlAppended, method, ContentType, data)
    {   
        ck = 'ck_000';
        cs = 'cs_000';
        url = 'http://myurl.com/wc-api/v3' + urlAppended;

        oauth = OAuth({
            consumer: {
                public: ck,
                secret: cs
            },
            signature_method: 'HMAC-SHA1'
        });

       request_data = {
          url: url,
          method: method,
        }

        params = oauth.authorize(request_data);
        console.log(params);

        return $http({
            url: request_data.url,
            method: request_data.method,
            params: params,
            data: data,
            dataType: "json",
            headers: {
                "Content-Type": ContentType
            }
        });

    }

    return {
        getData: getData
    }

})

This is a service that I have written to make requests to the API. Here is my POST request.

AuthService.getData('/customers/', 'post', 'application/json', data).then(function(response){
        console.log("Done" + response);
      })

Here the data is a JSON object. I always get the error.

XMLHttpRequest cannot load http://myurl.com/wc-api/v3/customers/?oauth_consumer_key=ck_eb42…th_signature_method=HMAC-SHA1&oauth_timestamp=1454961387&oauth_version=1.0. Response for preflight has invalid HTTP status code 401

Here is my .htaccess code

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

I have been trying to make it work but cannot do it. Any help is highly appreciated. Thanks.

Samarth Agarwal
  • 2,044
  • 8
  • 39
  • 79

0 Answers0