Caio Mar
Caio Mar

Reputation: 2624

React Native Woocommerce API fails when parameters are set

I recently decided to give React Native a shot. I am running into some trouble communicative with Woocommerce using the react-native-woocommerce-api.

When I get all the products, it works great, but when I try to set an attribute to the HTTP call, it fails.

This works:

componentDidMount() {
    console.log("Loading products...");
    Woocommerce.get("products", {}).then(response => {
        console.log(response);
    });
}

This doesn't (per_page added):

componentDidMount() {
    console.log("Loading products...");
    Woocommerce.get("products", {per_page: 3}).then(response => {
        console.log(response);
    });
}

Error thrown:

Object {
    "code": "woocommerce_rest_authentication_error",
    "data": Object {
        "status": 401,
    },
    "message": "Invalid signature - the signature doesn't match.",
}

HTTP request that works:

http://www.example.com/wp-json/wc/v3/products?oauth_consumer_key=###&oauth_nonce=###&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1560818379&oauth_version=1.0&oauth_signature=###=&

HTTP request that doesn't work:

http://www.example.com/wp-json/wc/v3/products?oauth_consumer_key=###&oauth_nonce=###&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1560817909&oauth_version=1.0&oauth_signature=###=&per_page=3

I should also add that when adding per_page=3 using Postman, it works. I don't know what difference it makes, the HTTP calls are very similar, it seems that only the uri parameters order are different.

Any help is appreciated! Been stuck on this the whole freaking day. :/

Upvotes: 0

Views: 3242

Answers (1)

Ali Mahami
Ali Mahami

Reputation: 41

Open react-native-woocommerce-api.js in node_modules\react-native-woocommerce-api\lib\

then goto line 195 (_getOAuth().authorize function) and change:

params.qs = this._getOAuth().authorize({
  url: url,
  method: method
});

to

params.qs = this._getOAuth().authorize({
  url: url + '?' + this.join(data, '&'),
  method: method
});

Upvotes: 1

Related Questions