Woocommerce API ionic 3 POST error 401


#1

Hi, i use woocommerce api on ionic app
(using nodejs module https://www.npmjs.com/package/woocommerce-api )

and all GET works but POST dosen’t !!

i’ve tried :

wc.postAsync('products', data).then((data) =>  {
       ...
}, (err) => { console.log('Error : ', err);  });

and i get 401 error

may any one help me to fix that ?
thanks :slight_smile:


WooCommerce API NPM implementation
#2

How are you testing? OS?
What is your ionic info output?
Do the GET requests also use authentication?


#3

in my website > wp-admin > woocommerce API > genearate KEY READ / Write
and then :

wc : any = WC({
  url: 'http://xxxxx.com',
  consumerKey: 'ck_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  consumerSecret: 'cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
});
wc.getAsync('products/categories").then((data) =>  {
      // THIS IS WORK FINE
}, (err) => { console.log('err', err); });

but POST NOT WORKING with 401 error
i tried with chrom simulation (android & ios)
and i built apk and installed on my Android phone

Note that on chrome I add plugin allow controll allow origin to make gets works.

ionic info

cli packages: (/usr/lib/node_modules)

@ionic/cli-utils  : 1.13.1
ionic (Ionic CLI) : 3.13.2

global packages:

cordova (Cordova CLI) : 7.0.1 

local packages:

@ionic/app-scripts : 3.0.0
Cordova Platforms  : android 6.2.3
Ionic Framework    : ionic-angular 3.7.1

System:

Android SDK Tools : 26.1.1
Node              : v6.11.2
npm               : 3.10.10 
OS                : Linux 4.9

Misc:

backend : legacy

#4

@Haroun did you solve this problem, I am getting the same error though the post is working from postman


#5

@Vidit i use a php library, and from my client side i make simple post to:
ww.mywebsite.com/my_api.php
in this file i make all my calls and authantication.

you can see the example here:


#6

Did you find solution ? for post data


#7

try with this one
wc : any = WC({

    url:"https://www.xxxxxx.com",
    consumerKey:"ck_xxxxxxx...................xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    consumerSecret:"cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    wpAPI : true,
    version:"wc/v2",
    queryStringAuth: true // Force Basic Authentication as query string true and using under HTTPS

});

#8

Thank you kind sir, this worked like a charm…but tell why did it work…
what did these three lines of code change

    wpAPI : true,
    version:"wc/v2",
    queryStringAuth: true