401未经授权的WooCommerce API中的Post方法

时间:2018-09-05 17:14:55

标签: javascript wordpress angular typescript woocommerce

首先,我将向您展示我的完整代码以了解该问题

这是woocommerce api身份验证,具有消费密钥和文件checkout.ts的秘密

      this.WooCommerce = WC({
        url:"http://localhost/wordpress/",
        consumerKey:"ck_429c9a4521b73420e6c6d57179ab2b19350fbdb8",
        consumerSecret:"cs_b9aaa8b11d15a6132abca0a8b5ca324fcfa76f87",
        

      });

这是函数placeOrder()的checkout.ts文件的订单的post方法。

      this.WooCommerce.postAsync("orders", orderData).then ((data) => {
        //console.log(JSON.parse(data.body).order);

        let response = (JSON.parse(data.body).order);

        this.alertCtrl.create({
          title: "Order Placed Successfully",
          message: "Your order has been placed successfully. Your order number is " + response.order_number,
          buttons: [{
            text: "OK",
            handler: () => {
              this.navCtrl.push(HomePage);
            }
          }]
        }).present();

      })

这是html文件checkout.html

      <ion-item>
        <ion-label>First Name</ion-label>
        <ion-input type="text" [(ngModel)]="newOrder.billing_address.first_name"></ion-input>
      </ion-item>
      
      ....
      
      <button ion-button block color="danger" (click)="placeOrder()">Place Order</button>

当我单击下订单按钮时​​,它会向我显示此错误

enter image description here

我在某个论坛中搜索了此问题,他们建议我在使用方密钥和秘密(如

)之后向woocommerce api auth添加一些代码
verifySsl: false,
queryStringAuth: true

还要在.httaccess文件中添加一些代码,例如

RewriteCond %{HTTP:Authorization} ^(.)
RewriteRule ^(.) - [E=HTTP_AUTHORIZATION:%1]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

并且我在控制台中仍然遇到相同的问题,这是netword的结果 enter image description here

1 个答案:

答案 0 :(得分:1)

var WooCommerce = new WooCommerceAPI({
  url: 'http://example.com', // Your store URL
  consumerKey: 'consumer_key', // Your consumer key
  consumerSecret: 'consumer_secret', // Your consumer secret
  wpAPI: true, // Enable the WP REST API integration
  version: 'wc/v2' // WooCommerce WP REST API version
});

因此,根据他们的documentation,您正在使用OAUTH 1.0授权策略,因此,以上是基本的WooCommerce对象签名和属性。