使用带有Java脚本的WooCommerce REST API创建新产品(客户端)?

时间:2016-03-17 作者:Guesto

是否可以在woocommerce中使用其API和客户端的javascript创建新产品?

我尝试了以下方法来获取所有产品:

端点:

https://localhost/wpShop/wc-api/v2/products?consumer_key=ck_1111111111122123&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1458225139&oauth_nonce=nVq4rX&consumer_secret=cs_232332322233232&oauth_signature=kzoVx+VYSWlLbRpi3f8222222=
javascript请求:

$.ajax({
  method: "GET",
  url: woocommerceURLProduct,
  data: ""
})
  .done(function( msg ) {
    console.log( "products: " );
    console.log( msg );
  });
这很有效!但我无法将此用于创建新产品。

相同端点:

https://localhost/wpShop/wc-api/v2/products?consumer_key=ck_1111111111122123&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1458225139&oauth_nonce=nVq4rX&consumer_secret=cs_232332322233232&oauth_signature=kzoVx+VYSWlLbRpi3f8222222=
我的新产品:

var data = {
    product: {
      title: \'Premium Quality\',
      type: \'simple\',
      regular_price: \'21.99\',
      description: \'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.\',
      short_description: \'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.\',
      categories: [
        9,
        14
      ],
      images: [
        {
          src: \'http://www.adpic.de/data/picture/detail/Wasserflasche_74756.jpg\',
          position: 0
        },
        {
          src: \'http://www.adpic.de/data/picture/detail/Wasserflasche_74756.jpg\',
          position: 1
        }
      ]
    }
  };
我的ajax请求:

$.ajax({
  method: "POST",
  url: woocommerceURLcreateProduct,
  data: $.param(data)
})
  .done(function( msg ) {
    console.log( "Data Saved: " );
    console.log( msg );
  });
I get the error:

{code: "woocommerce_api_missing_callback_param", message: "Missing Parameter data"}
资料来源:

http://woothemes.github.io/woocommerce-rest-api-docs/v2.html?javascript#create-a-product

1 个回复
SO网友:Foxtrot

如果您使用的是SSL(HTTPS URL),则不需要使用oauth1。0具有oauth签名和所有内容。您只需在url中传入consumer\\u密钥和consumer\\u密钥

https://localhost/wpShop/wc-api/v2/products?consumer_key=ck_1111111111122123&consumer_secret=cs_232332322233232
就是这样!然而,如果您的URL不是SSL,那么您就必须麻烦地创建HMAC-SHA1密钥等等。