我正在尝试使用woocommerce rest api通过PUT方法更新产品库存数量和价格,但是在运行代码后,它给了我错误:
禁止响应代码403。您无权访问/ wc-api / v2 / products / 15542 在此服务器上。
我使用的是centos 6服务器,我已经通过网络研究对modsecurity_crs_30_http_policy.conf [id“ 960032”]进行了评论。但仍然不起作用。
public void updateStock() {
String nonce = RandomStringUtils.randomAlphanumeric(32);
Map<String, String> paramMap = new TreeMap<>();
paramMap.put("oauth_consumer_key", key);
paramMap.put("oauth_timestamp", "" + (System.currentTimeMillis() /
1000));
paramMap.put("oauth_nonce", nonce);
paramMap.put("oauth_signature_method", "HMAC-SHA1");
List<NameValuePair> qparams = new ArrayList<>();
for (Map.Entry<String, String> parameter : paramMap.entrySet()) {
qparams.add(new BasicNameValuePair(parameter.getKey(),
parameter.getValue()));
}
String encodedParams =
URLEncoder.encode(URLEncodedUtils.format(qparams, ENC), ENC);
String signature = getSignature("http://topvacuumparts.com/wc-
api/v2/products/15542", encodedParams, "PUT");
qparams.add(new BasicNameValuePair("oauth_signature", signature));
HttpClient httpclient = HttpClientBuilder.create().build();
URI uri = URIUtils.createURI("http", "topvacuumparts.com", -1, "wc-
api/v2/products/15542", URLEncodedUtils.format(qparams, ENC), null);
HttpPut httpget = new HttpPut(uri);
httpget.setHeader("X-Stream", "true");
httpget.setHeader("Content-Type", "application/json");
JSONObject obj = new JSONObject();
JSONObject ob = new JSONObject();
obj.put("stock_quantity", "20");
ob.put("product", obj);
StringEntity entity1 = new StringEntity(obj.toString());
httpget.setEntity(entity1);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("Response Code " +
response.getStatusLine().getStatusCode());
}
这是结果: 响应码403 杰森回应 403禁止 您无权访问/ wc-api / v2 / products / 15542 在此服务器上