admin管理员组文章数量:1391937
I am facing problems trying to authenticate on WooCommerce Rest API, to do basic stuff like fetch products... I would like to do it in plain javascript, to use on a Cordova generated app. But I keep getting 401 Unauthorized
error when trying to fetch the content. Here is my code:
import axios from 'axios'
import OAuth from 'oauth-1.0a'
import crypto from 'crypto'
const ck = '[MY_CLIENT_KEY]'
const cs = '[MY_SECRET_KEY]'
const url = '[MY_URL]/wp-json/wc/v2/products'
const oauth = OAuth({
consumer: {
key: ck,
secret: cs
},
signature_method: 'HMAC-SHA1',
hash_function: function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64')
}
})
const token = {
key: ck,
secret: cs
}
var request_data = {
method: 'GET',
url: url
}
var params = oauth.authorize(request_data, token)
console.log(params)
axios.get(url + '/?oauth_signature=' + params.oauth_signature +
'&oauth_consumer_key=' + ck +
'&oauth_nonce=' + params.oauth_nonce +
'&oauth_signature_method=HMAC-SHA1&oauth_timestamp=' + params.oauth_timestamp +
'&oauth_token=' + params.oauth_token +
'&oauth_version=1.0')
.then(function(data){
console.log(data)
}, function(error){
console.log(error)
})
Any ideas on how to get this done? Where am I failing?
I am facing problems trying to authenticate on WooCommerce Rest API, to do basic stuff like fetch products... I would like to do it in plain javascript, to use on a Cordova generated app. But I keep getting 401 Unauthorized
error when trying to fetch the content. Here is my code:
import axios from 'axios'
import OAuth from 'oauth-1.0a'
import crypto from 'crypto'
const ck = '[MY_CLIENT_KEY]'
const cs = '[MY_SECRET_KEY]'
const url = '[MY_URL]/wp-json/wc/v2/products'
const oauth = OAuth({
consumer: {
key: ck,
secret: cs
},
signature_method: 'HMAC-SHA1',
hash_function: function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64')
}
})
const token = {
key: ck,
secret: cs
}
var request_data = {
method: 'GET',
url: url
}
var params = oauth.authorize(request_data, token)
console.log(params)
axios.get(url + '/?oauth_signature=' + params.oauth_signature +
'&oauth_consumer_key=' + ck +
'&oauth_nonce=' + params.oauth_nonce +
'&oauth_signature_method=HMAC-SHA1&oauth_timestamp=' + params.oauth_timestamp +
'&oauth_token=' + params.oauth_token +
'&oauth_version=1.0')
.then(function(data){
console.log(data)
}, function(error){
console.log(error)
})
Any ideas on how to get this done? Where am I failing?
Share Improve this question asked Aug 2, 2017 at 14:27 Eduardo C. K. FerreiraEduardo C. K. Ferreira 3743 silver badges13 bronze badges2 Answers
Reset to default 6Got it, inspired by this answer here.
import axios from 'axios'
import OAuth from 'oauth-1.0a'
import CryptoJS from 'crypto-js'
import jQuery from 'jquery'
const that = this
const ck = '[MY_CLIENT_KEY]'
const cs = '[MY_SECRET_KEY]'
const url = '[MY_URL]/wp-json/wc/v2/products'
const 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));
}
});
const requestData = {
url: url,
method: 'GET'
};
axios.get(
requestData.url + '?' + jQuery.param(oauth.authorize(requestData))
).then(function(response){
console.log(response.data)
}, function(error){
console.log(error)
})
I don't think you need jquery to add the params in Axios I think you can just do
axios.get(requestData.url, { params: oauth.authorize(requestData) }).then(
function(response) {
console.log(response.data);
},
function(error) {
console.log(error);
}
);
本文标签: WooCommerce Rest API Oauth with JavascriptStack Overflow
版权声明:本文标题:WooCommerce Rest API Oauth with Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744703948a2620726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论