@ApiOperation("上传电子发票文件") @PostMapping("uploadFapiaoFile") public Result uploadFapiaoFile() throws IOException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException, InvalidKeyException { String url = wxPay.getDomain() + "/v3/new-tax-control-fapiao/fapiao-applications/upload-fapiao-file"; HttpUrl parse = HttpUrl.parse(url); String path = "E:\\online-consultation\\处方.pdf"; String sm3 = FileUtils.sm3(path); // String sm3 = FileUtils.sm3(path); log.info("SM3[{}]", sm3); JSONObject meta = new JSONObject(); meta.put("file_type", "PDF"); meta.put("digest", sm3); meta.put("digest_algorithm", "SM3"); String signatureString = WxSignatureUtil.getSignatureString("POST", parse, meta.toString()); log.info("signature[{}]", signatureString); MultipartBody multipartBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("meta", "meta", RequestBody.create(MediaType.parse("application/json"), meta.toString())) .addFormDataPart("file", "file", RequestBody.create(new File(path), MediaType.parse("pdf/plain"))) .build(); OkHttpClient client = new OkHttpClient(); Request build = new Request.Builder() .addHeader("Content-Type", "multipart/form-data") .addHeader("Authorization", signatureString) .addHeader("Accept", "application/json") .url(url) .post(multipartBody) .build(); Response response = client.newCall(build).execute(); ResponseBody responseBody = response.body(); String string = responseBody.string(); return Result.ok(string); } public static String sm3(String path) { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); try (FileInputStream fis = new FileInputStream(path); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { baos.write(buffer, 0, bytesRead); } SM3.Digest sm3 = new SM3.Digest(); sm3.update(baos.toByteArray()); byte[] digest = sm3.digest(); return Hex.toHexString(digest); } catch (Exception e) { throw new RuntimeException(e); } } @Slf4j public class WxSignatureUtil { private static String serial = ""; private static String mchid = ""; public static String getSignatureString(String method, HttpUrl url, String body) throws UnsupportedEncodingException, NoSuchAlgorithmException, SignatureException, InvalidKeyException { String nonceStr = RandomStrUtils.getRandomString(32); long timestamp = System.currentTimeMillis() / 1000; String message = buildMessage(method, url, timestamp, nonceStr, body); String signature = sign(message.getBytes("utf-8")); log.info("签名值:[{}]", signature); return "WECHATPAY2-SHA256-RSA2048 mchid=\"" + mchid + "\"," + "nonce_str=\"" + nonceStr + "\"," + "timestamp=\"" + timestamp + "\"," + "serial_no=\"" + serial + "\"," + "signature=\"" + signature + "\""; } public static String sign(byte[] message) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException { Signature sign = Signature.getInstance("SHA256withRSA"); InputStream resourceAsStream = WxSignatureUtil.class.getClassLoader().getResourceAsStream("apiclient_key.pem"); sign.initSign(PemUtil.loadPrivateKey(resourceAsStream)); sign.update(message); return Base64.getEncoder().encodeToString(sign.sign()); } public static String buildMessage(String method, HttpUrl url, long timestamp, String nonceStr, String body) { String canonicalUrl = url.encodedPath(); if (url.encodedQuery() != null) { canonicalUrl += "?" + url.encodedQuery(); } StringBuilder sb = new StringBuilder(method + "\n" + canonicalUrl + "\n" + timestamp + "\n" + nonceStr + "\n"); if (!StringUtils.isEmpty(body)) { sb.append(body + "\n"); } else { sb.append(""+"\n"); } String str = sb.toString(); log.info("构造签名体:[{}]", str); return str; } } { "code": "SIGN_ERROR", "detail": { "detail": { "issue": "sign not match" }, "field": "signature", "location": "authorization", "sign_information": { "method": "POST", "sign_message_length": 116, "truncated_sign_message": "POST\n/v3/new-tax-control-fapiao/fapiao-applications/upload-fapiao-file\n1722331061\nHp19Fkd3BaYXp3rMXSjzA88wJCT5GE32\n\n", "url": "/v3/new-tax-control-fapiao/fapiao-applications/upload-fapiao-file" } }, "message": "错误的签名,验签失败" }我也遇到这种情况,兄弟你解决了吗
使用wechatpay-apiv3创建client调用上传电子发票文件接口,一直验签失败1.创建client,在wechatpay-go官方库说,使用option.WithWechayPayAutoAuthCipher会自动帮忙验签[图片]2.在使用post,调用/v3/new-tax-control-fapiao/fapiao-applications/upload-fapiao-file这个接口,一直验签失败。在truncated_sign_message中没有看到有请求体[图片]。[图片]。请问应该如何使用POST调用上传电子发票文件接口
07-30