Woocommerce API获取所有产品 [英] Woocommerce API get all products

查看:108
本文介绍了Woocommerce API获取所有产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从API获取具有某些参数的产品.我使用了 WooCommerce API Java包装器.带有OAuth 1.0的REST API.简单的getAll方法返回一页的列表(10个产品).为了获得全部,我必须设置一页中必须有多少产品,并使用偏移量.要获取第三页,必须发送以下参数:"per_page = 10& offset = 20".我在get& post程序中使用查询进行测试-所有工作均正常进行.在Java中,当我添加参数时-我收到错误(401)-无效的签名-提供的签名不匹配".我更改了WooCommerceAPI类:

I tried to get product from API with some parameters. I used WooCommerce API Java Wrapper. REST API with OAuth 1.0. Simple getAll method return list of one page (10 products). To get all i must set how much products must be in one page and use offset. To get third page must send this parameters: "per_page=10&offset=20". I test with query in get&post programm - all work. In Java, when i added parameters - i got error (401)- "Invalid signature - the provided signature did not match". I changed WooCommerceAPI class:

private static final String API_URL_FORMAT = "%s/wp-json/wc/v2/%s";
    private static final String API_URL_ONE_ENTITY_FORMAT = "%s/wp-json/wc/v2/%s/%d";
    private HttpClient client;
    private OAuthConfig config;
 public List getAll(String endpointBase) {
        String url = String.format(API_URL_FORMAT, config.getUrl(), endpointBase) + "?per_page=10&offset=20";
        String signature = OAuthSignature.getAsQueryString(config, url, HttpMethod.GET);
        String securedUrl = String.format("%s&%s", url, signature);
        System.out.println("url="+url);
        System.out.println("securedUrl="+securedUrl);
        return client.getAll(securedUrl);
    }

但是我有同样的错误.

推荐答案

我刚刚发布了wc-api-java库的新版本(1.2版),现在您可以使用带有params参数的getAll方法了.放置其他请求参数.例如:

I've just released a new version of wc-api-java library (version 1.2) and now you can use the method getAll with params argument where you can put additional request parameters. For example:

// Get all with request parameters
Map<String, String> params = new HashMap<>();
params.put("per_page","100");
params.put("offset","0");
List products = wooCommerce.getAll(EndpointBaseType.PRODUCTS.getValue(), params);

System.out.println(products.size());

这篇关于Woocommerce API获取所有产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆