使用带有http和javascript(而不是https)的woocommerce rest api v1 [英] Use woocommerce rest api v1 with http and javascript (not https)

查看:230
本文介绍了使用带有http和javascript(而不是https)的woocommerce rest api v1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 oauth-signature 生成用于与woocommerce api连接的oauth签名。我按照 woocommerce rest api documentation 中所述的所有步骤进行了操作a>:


所需参数为: oauth_consumer_key,oauth_timestamp,oauth_nonce,oauth_signature和oauth_signature_method 。 oauth_version不是必需的,应该省略。
OAuth nonce可以是任意随机生成的32个字符(推荐)字符串,对于使用者密钥是唯一的。等等......


但是以下请求仍然未经授权返回:



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为WC激活的API,为WC等激活的SSL。



我还检查了这是否按照库的要求完成:


使用签名基本字符串和您的消费者密钥生成签名,并使用&使用HMAC-SHA1散列算法的字符。


时区是UNIX,应根据需要生成随机数。那么你们有些人发现了这个问题吗?这是我的代码:

  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
},
//生成RFC 3986编码,BASE64编码的HMAC-SHA1哈希
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));
}
返回文字;
}

其他人有运气吗?

解决方案

我终于让它工作了。



不知何故它不能用于我当地的wordpress安装,但是它确实适用于我的live wordpress网站:



Angular2代码:

 构造函数(私有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 install crypto-js --save
npm install oauth-1.0a --save



必填文件:

 脚本:[
.. /node_modules/crypto-js/crypto-js.js,
../ node_modules/oauth-1.0a/oauth-1.0a.js
]


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:

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...

But the following request still returns unauthorized:

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

(Dont worry, the keys are only for local use)

Response:

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

Im using WP 4.7, WC 2.6.9, API Activated for WC, SSL Deactivated for WC etc..

I also checked that this is done as required by the library:

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

The timezone is UNIX, and the nonce should be generated as required. So does some of you spot the problem? Here is my code:

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

Anyone else had any luck with this?

解决方案

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"
  ]

这篇关于使用带有http和javascript(而不是https)的woocommerce rest api v1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆