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

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

问题描述

我尝试使用一些参数从 API 获取产品.我使用了 WooCommerce API Java Wrapper.带有 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);
    }

但我遇到了同样的错误.

But I have got the same error.

推荐答案

我刚刚发布了一个新版本的 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天全站免登陆