calatonsystems/wc-api-java

Not able to get orders between dates

kiranuw opened this issue · 3 comments

Hi,

I wanted to get woo-commerce orders between 2 dates. I tried before and after params but it is throwing the below error. please help me .
Code :
WooCommerce wooCommerce = new WooCommerceAPI(config, ApiVersionType.V3);

		// Get all with request parameters
		Map<String, String> params = new HashMap<>();

// params.put("per_page", "1");
params.put("after", "2021-03-01T00:00:00");
params.put("before", "2021-02-16T23:59:59");

		List<Map<String, Object>> orders = wooCommerce.getAll(EndpointBaseType.ORDERS.getValue(), params);

Error
java.lang.RuntimeException: Can't parse retrieved object: {code=woocommerce_rest_authentication_error, message=Invalid signature - provided signature does not match., data={status=401}}

I get the same problem as well. Calling the woo-commerce endpoint via CURL is fine, for example:

curl "https://www.mysite.com/wp-json/wc/v2/orders?after=2021-01-01T00:00:00.000Z&before=2021-01-20T00:00:00.000Z" -u key:secret;

Furthermore, using this parameters is working like a charm as well:

// params.put("after",  "2021-02-01T00:00:00.000Z");
        // params.put("before", "2021-02-13T00:00:00:000Z");
        params.put("context", "view");
        params.put("per_page", "20");
        params.put("page", "1");
        params.put("status", "any");
        params.put("orderby", "title");

Only when we uncomment one of before or after, then we get the exception described in this issue. Maybe there's little things that missing here.

@omandryk (sorry for mentioned you here), but do you have any idea what's going on?

Issue Fixed. Got the solution we need to encode the dates.

String wcFormattedDate=startDate+"T00:00:00";
			Map<String, String> params = new HashMap<>();
			params.put("per_page", "10");
			params.put("after", percentEncode(wcFormattedDate));

	public String percentEncode(String s) {
		final String UTF_8 = "UTF-8";

		try {
			return URLEncoder.encode(s, UTF_8)
					// OAuth encodes some characters differently:
					.replace(SpecialSymbol.PLUS.getPlain(), SpecialSymbol.PLUS.getEncoded())
					.replace(SpecialSymbol.STAR.getPlain(), SpecialSymbol.STAR.getEncoded())
					.replace(SpecialSymbol.TILDE.getEncoded(), SpecialSymbol.TILDE.getPlain());
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}


@kiranuw I think this issue should not closed yet, as currently there's PR from me about this issue.