【发布时间】:2017-05-08 11:34:51
【问题描述】:
我正在使用 oauth-signature 生成我的 oauth 签名以连接 woocommerce api。我按照woocommerce rest api documentation 中所述的所有步骤进行操作:
必需的参数是:oauth_consumer_key、oauth_timestamp、oauth_nonce、oauth_signature 和 oauth_signature_method。 oauth_version 不是必需的,应该省略。 OAuth 随机数可以是任何随机生成的 32 个字符(推荐)的字符串,该字符串对使用者密钥是唯一的。等等……
但以下请求仍然返回未授权:
(别担心,钥匙仅供本地使用)
回复:
{"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,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;
}
其他人对此有任何运气吗?
【问题讨论】:
-
我在这里找到了一个“不起作用”的解决方案:github.com/woocommerce/woocommerce/issues/6493 但这对我不起作用。 quote => "我将github.com/bettiolo/oauth-signature-js/blob/master/dist/… 替换为 this._key = consumerSecret;"
-
(看起来他使用的是 wc api 'legacy v2' 而我使用的是最新的 'v1')^
标签: http angular oauth woocommerce-rest-api oauth-1.0a