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

前端 未结 2 1122
时光取名叫无心
时光取名叫无心 2021-01-26 11:18

Im using oauth-signature to generate my oauth-signature for connection with woocommerce api. I followed all the steps stated at woocommerce rest api documentation:

2条回答
  •  囚心锁ツ
    2021-01-26 11: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"
      ]
    

提交回复
热议问题