将 woocommerce rest api v1 与 http 和 javascript(不是 https)一起使用

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

我正在使用 oauth-signature to generate my oauth-signature for connection with woocommerce api. I followed all the steps stated at woocommerce rest api documentation:

The required parameters are: oauth_consumer_key, oauth_timestamp, oauth_nonce, oauth_signature, and oauth_signature_method. oauth_version is not required and should be omitted. The OAuth nonce can be any randomly generated 32 character (recommended) string that is unique to the consumer key. etc...

但以下请求仍然returns未经授权:

http://siglar.no/wp-json/wc/v1/orders?oauth_consumer_key=ck_1ca1c6ff1a93de4836ee52c766538043d7f15d07&oauth_timestamp=1482431903&oauth_nonce=P5SM1FGeFVpdRyHWp4HHYOMlYAhxE6Gl&oauth_signature=cEETZUnSNQD6uorII9c%2B5SXf0M8%3D&oauth_signature_method=HMAC-SHA1

(别担心,密钥只供本地使用)

回复:

{"code":"woocommerce_rest_cannot_view","message":"Beklager, du kan ikke liste ressurser.","data":{"status":401}}

我正在使用 WP 4.7、WC 2.6.9,API WC 已激活,WC 已停用 SSL 等。

我还检查了这是按照图书馆的要求完成的:

Generate the signature using the signature base string and your consumer secret key with a & character with the HMAC-SHA1 hashing algorithm.

时区为UNIX,需要根据需要生成nonce。那么你们中的一些人发现问题了吗?这是我的代码:

constructor(private http: Http) {

    var d = new Date();
    var httpMethod = 'GET',
        url = 'http://siglar.no/wp-json/wc/v1/orders',
        ck = 'ck_1ca1c6ff1a93de4836ee52c766538043d7f15d07',
        cs = 'cs_ce323425064c37688d614e4ff43a5489c6f78017',
        sm = 'HMAC-SHA1',
        nc = this.nonceGen(),
        timestamp = Math.floor(d.getTime()/ 1000),
        parameters = {
            oauth_consumer_key : ck,
            oauth_timestamp : timestamp,
            oauth_nonce : nc,
            oauth_signature_method : sm
        },
        // generates a RFC 3986 encoded, BASE64 encoded HMAC-SHA1 hash
        encodedSignature = oauthSignature.generate(httpMethod, url, parameters, cs);

    this.http.get(
        url + '?oauth_consumer_key='+ck+'&oauth_timestamp='+timestamp+'&oauth_nonce='+nc+'&oauth_signature='+encodedSignature+'&oauth_signature_method='+sm
    ).subscribe(data => {
        console.log('fetched');
        console.log(data);
    });

}

public nonceGen() {
    let length = 32;
    let text = "";
    let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    for(let i = 0; i < length; i++) {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
    return text;
}

还有其他人对此有任何好运吗?

我终于成功了。

不知何故,它不适用于我的本地 wordpress 安装,但它确实适用于我的实时 wordpress 站点:

Angular2 代码:

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);
    });

}

使用的库(通过 npm 安装):

npm 安装 crypto-js --save npm 安装 oauth-1.0a --save

所需文件:

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

我们遇到了 401 unauthorised 响应的问题,响应代码如 woocommerce_rest_invalid_signature

结果发现我们hash的时候用的URL和我们访问的URL不一样,hash用的URL是漏了尾部斜杠.