wechatpay-apiv3 / wechatpay-apache-httpclient

微信支付 APIv3 Apache HttpClient装饰器(decorator)
https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/index.shtml
Apache License 2.0
665 stars 249 forks source link

【Help】使用demo中的例子上传图片返回400 badrequest #188

Closed zzmloveddm closed 1 year ago

zzmloveddm commented 1 year ago

可否帮忙看下这个是啥情况 代码如下: PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(Files.newInputStream(Paths.get(PrivateKeyPath))); //使用自动更新的签名验证器,不需要传入证书 verifier = new AutoUpdateCertificatesVerifier( new WechatPay2Credentials(tMerchantId, new PrivateKeySigner(MerchantSerialNumber, merchantPrivateKey)), ApiV3key.getBytes(StandardCharsets.UTF_8)); httpClient = WechatPayHttpClientBuilder.create() .withMerchant(MerchantId, MerchantSerialNumber, merchantPrivateKey) .withValidator(new WechatPay2Validator(verifier)) .build(); URI uri = new URI("https://api.mch.weixin.qq.com/v3/merchant/media/upload"); byte[] bytes = file.getBytes(); try (InputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) { String sha256 = DigestUtils.sha256Hex(byteArrayInputStream); WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(uri) .withImage(file.getOriginalFilename(), sha256, byteArrayInputStream) .build(); Header[] allHeaders = request.getAllHeaders(); for (Header allHeader : allHeaders) { System.out.println(allHeader); } try (CloseableHttpResponse response = httpClient.execute(request)) { if (SC_OK == response.getStatusLine().getStatusCode()){ HttpEntity entity = response.getEntity(); // do something useful with the response body // and ensure it is fully consumed String s = EntityUtils.toString(entity); System.out.println(s); return success; }else { return error; } } } 返回400 badrequest

xy-peng commented 1 year ago

你看下服务器返回的错误码和错误信息?

zzmloveddm commented 1 year ago

你看下服务器返回的错误码和错误信息?

应答的状态码不为200-299。status code[400] request headers[[Accept: application/json; charset=UTF-8, Authorization: WECHATPAY2-SHA256-RSA2048 mchid="我的appId",nonce_str="xOWyRfSwKYc63dPWQjxntu9ASfdEjJDy",timestamp="1676433245",serial_no="我的证书序列号",signature="nz0UdFsD0pRw5g4/kYnB0sCvdky8mmG+ZvrerZ68Wj8NTmFNZ2bDebmGneUQtu6Mvdeif1dOpBkzM+DpGra6cbkhQnEO1zSZRtIuo1wbLp9lo/oSIwDQXf5h81YDyvmSTDJ7x5qwI1YbTVVALLu4vDjILkEoNjNTHQfGEYbBeJey39DBuS/5hgIHDCDvESa6tofaX+Ox1onqmQgcqrG6+nvm6B1t1VGH5zA62Q4JiAbQ4gU6z7X108WEoETbuOkjIWDYUZ78bXkxvEEgqCWCV51DrybZ9QLxRRlWokK9dk8pNnnI8vA4Xa9CwtLoGyu07Co8p8NElN1P1uQshniUdg==", Transfer-Encoding: chunked, Content-Type: multipart/form-data; boundary=tZYqbmWlPGw2UR6hFQMjgSvraLAaDBKhEy, Host: api.mch.weixin.qq.com, Connection: Keep-Alive, User-Agent: WechatPay-Apache-HttpClient/0.4.9 (Windows 10/10.0) Java/1.8.0_152, Accept-Encoding: gzip,deflate]]

xy-peng commented 1 year ago

400只是状态码。你需要把应答流收回来,输出 body,看到错误码和错误信息。它们可以用来定位问题。

zzmloveddm commented 1 year ago

400只是状态码。你需要把应答流收回来,输出 body,看到错误码和错误信息。它们可以用来定位问题。

好的谢谢