Woocommerce REST API
Hallo,
Ik ben me wat het verdiepen in de Rest Api techniek van Woocommerce / Wordpress.
Nu zie ik alleen maar op het internet voorbeelden met Postman, weet iemand hoe ik dit ook met een AJAX call zou kunnen doen?
Ik ben me wat het verdiepen in de Rest Api techniek van Woocommerce / Wordpress.
Nu zie ik alleen maar op het internet voorbeelden met Postman, weet iemand hoe ik dit ook met een AJAX call zou kunnen doen?
Postman is ook puur bedoeld om REST API's mee te testen. Dus je kan de endpoint-URL's prima in je JavaScript code verwerken.
Ok, tot nu toe heb ik dit
Als ik in de console kijk krijg ik
Op internet vind ik dat de authenticatie niet goed is.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const wooClientKey = 'xxxxxxx';
const wooClientSecret = 'xxxxxx';
const wooUrl = 'https://www.xxxxxx.com/wp-json/wc/v3/orders';
function basicAuth(key, secret) {
let hash = btoa(key + ':' + secret);
return "Basic " + hash;
}
let auth = basicAuth(wooClientKey, wooClientSecret);
function getData(url) {
jQuery.ajax({
url: url,
method: 'GET',
beforeSend: function (req) {
req.setRequestHeader('Authorization', auth);
}
})
.done(function (data) {
console.log(data);
return data;
});
}
getData(wooUrl);
const wooClientSecret = 'xxxxxx';
const wooUrl = 'https://www.xxxxxx.com/wp-json/wc/v3/orders';
function basicAuth(key, secret) {
let hash = btoa(key + ':' + secret);
return "Basic " + hash;
}
let auth = basicAuth(wooClientKey, wooClientSecret);
function getData(url) {
jQuery.ajax({
url: url,
method: 'GET',
beforeSend: function (req) {
req.setRequestHeader('Authorization', auth);
}
})
.done(function (data) {
console.log(data);
return data;
});
}
getData(wooUrl);
Als ik in de console kijk krijg ik
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
{
"code": "woocommerce_rest_cannot_view",
"message": "Je kunt gegevensbronnen niet weergeven.",
"data": {
"status": 401
}
}
"code": "woocommerce_rest_cannot_view",
"message": "Je kunt gegevensbronnen niet weergeven.",
"data": {
"status": 401
}
}
Op internet vind ik dat de authenticatie niet goed is.
Zie ook:
http://woocommerce.github.io/woocommerce-rest-api-docs/#authentication
Probeer het eens met dit:
https://www.xxxxxx.com/wp-json/wc/v3/orders?consumer_key=X&consumer_secret=X
Met uiteraard jouw key's meegeven.
Check ook je networktab eens!
http://woocommerce.github.io/woocommerce-rest-api-docs/#authentication
Probeer het eens met dit:
https://www.xxxxxx.com/wp-json/wc/v3/orders?consumer_key=X&consumer_secret=X
Met uiteraard jouw key's meegeven.
Check ook je networktab eens!
Yes dit werkte inderdaad:)
Ik moet deze code natuurlijk alleen in een beveiligde omgeving plaatsen?
Want anders kan natuurlijk iedereen je sleutels zien toch?
Ik moet deze code natuurlijk alleen in een beveiligde omgeving plaatsen?
Want anders kan natuurlijk iedereen je sleutels zien toch?