Skip to content

Not able to get orders between dates #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kiranuw opened this issue Mar 8, 2021 · 3 comments
Closed

Not able to get orders between dates #36

kiranuw opened this issue Mar 8, 2021 · 3 comments

Comments

@kiranuw
Copy link

kiranuw commented Mar 8, 2021

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}}

@xsalefter
Copy link

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?

xsalefter added a commit to xsalefter/wc-api-java that referenced this issue Mar 18, 2021
@kiranuw
Copy link
Author

kiranuw commented Mar 22, 2021

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 kiranuw closed this as completed Mar 22, 2021
@xsalefter
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants