Ionic 3 WP-REST API Post request 401 (unauthorized

2019-08-20 17:16发布

问题:

I am developing a simple application to create a user in Wordpress (woocommerce) through wp-rest api. but when I try to create a new customers it shows 401 Unauthorised error in console. heres is my code for request.

signup(){

    let customerData = {
      customer : {}
    }

    customerData.customer = {
      "email": this.newUser.email,
      "first_name": this.newUser.first_name,
        ...
      "billing_address": {
        "first_name": this.newUser.first_name,
        ...
      },
      "shipping_address": {
        "first_name": this.newUser.first_name,
          ...

      }
    }

    if(this.billing_shipping_same){
      this.newUser.shipping_address = this.newUser.shipping_address;
    }
    this.WooCommerce.postAsync('customers', customerData).then( (data) => {

      console.log(JSON.parse(data.body));
    })

  }

by the way the key client & secret are correct

回答1:

You need an https connection, add the following lines to your woocommerce init :

verifySsl: false,
queryStringAuth: true

Any post request requires an https connection.