angularjs - WC API REST can't POST data oauth 1.0 -
i'm creating angularjs client wp api rest , wc api rest , i've got problem posting data orders. 401 error unauthorized. can tell me i'm doing wrong?
angular.module('egonometriaapp') .service('$order', ['$http', function ($http) { var order={ payment_method: 'bacs' }; function randomstring(length, chars) { var result = ''; (var = length; > 0; --i) result += chars[math.round(math.random() * (chars.length - 1))]; return result; } var consumer_key = "ck_ae3c4951e083094b8ae716c3b8b59eac4a35515b", httpmethod = "post", url = "http://it-grafika.pl:8080/wp-json/wc/v2/orders", parameters = { oauth_consumer_key: consumer_key, oauth_signature_method: "hmac-sha1", oauth_timestamp: (math.floor((new date().gettime()) / 1000)).tostring(), oauth_nonce: randomstring(32, "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz") }, consumersecret = "cs_00af1fb1fa20bcf81d3c68dc8cfea4f3b9f261e9", tokensecret = "", encodedsignature = oauthsignature.generate(httpmethod, url, parameters, consumersecret, tokensecret, {encodesignature: true}); console.log(encodedsignature); parameters['oauth_signature'] = encodedsignature; console.log(parameters); this.postorder = function () { var config = { method: httpmethod, url: url, headers: { authorization: 'oauth '+ 'oauth_consumer_key="'+parameters['oauth_consumer_key']+'",oauth_signature_method="'+parameters['oauth_signature_method']+ '",oauth_timestamp="'+parameters['oauth_timestamp']+'",oauth_nonce="'+parameters['oauth_nonce']+'",oauth_version="1.0"'+',oauth_signature="'+parameters['oauth_signature']+'"', "content-type": 'content-type: application/json' } // data: order }; $http(config) .then(function (status) { console.log(status); }); }; }]) ;
it working when i'm using postman. no idea why authorization isn't passing thru.
this in browser http://imgur.com/a/bemc8
since making cross domain request, need pass withcredentials true in config.
your config should this.
var config = { "async": true, "crossdomain": true, method: httpmethod, url: url, withcredentials : true, headers: { authorization: 'oauth '+ 'oauth_consumer_key="'+parameters['oauth_consumer_key']+'",oauth_signature_method="'+parameters['oauth_signature_method']+ '",oauth_timestamp="'+parameters['oauth_timestamp']+'",oauth_nonce="'+parameters['oauth_nonce']+'",oauth_version="1.0"'+',oauth_signature="'+parameters['oauth_signature']+'"', "content-type": 'content-type: application/json' } please refer https://developer.mozilla.org/en-us/docs/web/http/access_control_cors#requests_with_credentials more details.
Comments
Post a Comment