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

前端 未结 2 1123
时光取名叫无心
时光取名叫无心 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"
      ]
    
    0 讨论(0)
  • 2021-01-26 12:14

    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.

    0 讨论(0)
提交回复
热议问题