Use woocommerce rest api v1 with http and javascript (not https)

白昼怎懂夜的黑 提交于 2019-12-02 09:43:54

I did finally get it working.

Somehow it wouldnt work for my local wordpress installation, but it did work for my live wordpress site:

Angular2 code:

constructor(private http: Http) {

    var oauth = OAuth({
        consumer: {
            key: 'ck_...',
            secret: 'cs_...'
        },
        signature_method: 'HMAC-SHA1',
        hash_function: function(base_string, key) {
            return CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(base_string, key));
        }
    });

    var requestData = {
        url: 'http://siglarweb.no/wp-json/wc/v1/orders',
        method: 'GET'
    };

    this.http.get(
        requestData.url + '?' + jQuery.param(oauth.authorize(requestData))
    ).subscribe(data => {
        console.log(data);
    });

}

libraries used (installed via npm):

npm install crypto-js --save npm install oauth-1.0a --save

Required files:

"scripts": [
    "../node_modules/crypto-js/crypto-js.js",
    "../node_modules/oauth-1.0a/oauth-1.0a.js"
  ]

We have met issues with a 401 unauthorised response with response code like woocommerce_rest_invalid_signature.

And it turns out that the URL we used when hashing is different from the URL we accessed, the URL used in the hash is missing the trailing slash.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!