简体   繁体   English

在node.js中验证WooCommerce REST API

[英]Authenticate WooCommerce REST API in node.js

Okay so I'm VERRY VERRY VERRY new to the subject so please go easy on me. 好的,所以我是VERRY VERRY VERRY的新手,所以请对我轻松一点。 What I have done is to create a wordpress woocommerce website. 我所做的就是创建一个wordpress woocommerce网站。 What I want to do now is to get product-data from the wordpress site in node.js. 我现在要做的是从node.js中的wordpress网站获取产品数据。 If I have understood everything correctly i first have to Authenticate myself. 如果我正确理解了所有内容,那么我首先必须对自己进行身份验证。

First i installed the WooCommerce package with: 首先,我使用以下方法安装了WooCommerce软件包:

npm install --save woocommerce-api

Then i tried to Authenticante like this: 然后我尝试像这样去Authenticante:

    var WooCommerce = require('woocommerce');

var wooCommerce = new WooCommerce({
  consumerKey: 'ck_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  consumerSecret: 'cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  url: 'http://localhost/wordpress',


});

I realise It's an verry open question and i might be far of from on the right track. 我意识到这是一个非常开放的问题,我可能离正确的道路还很远。 But I would appriciate some tips or guiding. 但是我会建议一些技巧或指导。

Thanks! 谢谢!

Well, you npm installed 'woocommerce-api', so you need to require that, not 'woocommerce'. 好吧,您npm安装了“ woocommerce-api”,因此您需要这样做,而不是“ woocommerce”。

var WooCommerceAPI = require('woocommerce-api');

Refer to the documentation, but it looks like you instantiated mostly correctly. 请参考文档,但是看起来您几乎正确地实例化了。 If you're using the WP REST API you'll need to include the keys wpAPI and version, as per the library's documentation: https://www.npmjs.com/package/woocommerce-api 如果您使用的是WP REST API,则需要根据库的文档包括密钥wpAPI和版本: https ://www.npmjs.com/package/woocommerce-api

Now, if you want to get products, you would do something like this: 现在,如果要获得产品,您可以执行以下操作:

wooCommerce.getAsync('products').then(function(result) {
  return JSON.parse(result.toJSON().body);
});

Or you can use .get instead of .getAsync if you'd prefer to use callbacks over promises. 或者,如果您希望使用回调而不是Promise,则可以使用.get而不是.getAsync。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM