文件转换 报30003错误, signature not match 求助

阅读次数 82

我在postman中测试可以通过 但是代码中就报错 {"code":30003,"message":"InvalidSignature","hint":"signature not match. expect: 75b134a375868398335721531fd2f8a3afc8acaf, actual: d39317d513fc31a273220c3fdf51f864931436fe","extra":""}

,看了下postman中的body参数md5加密后与后台加密后数据不一样,请问怎么回事

 Map param = new LinkedHashMap<>();
         param.put("url", "http://www.xxxxx.com/userfiles/xxxxx/test11.docx");
         param.put("filename", "test11.docx");
         String content = JSON.toJSONString(param);
 String contentMd5 = Common.getMd52(content);

 okhttp3.MediaType mediaType=okhttp3.MediaType.parse(mediaTypeStr);
 RequestBody body = RequestBody.create(mediaType,content);
 OkHttpClient client = new OkHttpClient();
 Request request = new Request.Builder()
			.url("https://solution.wps.cn/api/developer/v1/office/save/as/docx")
			.post(body) 
			.addHeader("Date", headerDate)
			.addHeader("Content-Md5", contentMd5)
			.addHeader("Content-Type", mediaTypeStr)
			.addHeader("Authorization", "WPS-2:"+convertAppid+":"+seed) //
			.build();


public static String getMd52(String value) {
	    	String hexString ="";
	        System.out.println(value);
	        MessageDigest md;
			try {
				md = MessageDigest.getInstance("MD5");
              // 计算消息的摘要
              byte[] digest = md.digest(value.getBytes());

              // 将摘要转换为十六进制字符串
              hexString = bytesToHex(digest);

              System.out.println(hexString);
              } catch (NoSuchAlgorithmException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              return hexString;
	    }

	    public static String bytesToHex(byte[] bytes) {
	        StringBuilder hexString = new StringBuilder();
	        for (byte b : bytes) {
	            String hex = Integer.toHexString(0xff & b);
	            if (hex.length() == 1) {
	                hexString.append('0');
	            }
	            hexString.append(hex);
	        }
	        return hexString.toString();
	    }
4 Answers

可以打印一下用于计算signature的所有参数对比一下。有一些框架的Content_Type可能有些特殊添加。

我参数贴楼下了 麻烦老师帮忙看下 有什么地方不对么

但是postman里面的md5值不一样 Date: Tue, 07 Nov 2023 09:18:44 GMT Content-Type: application/json Content-MD5: ec9f194f3e742fac4bed527b37a036ff

你有抓包工具吗?你可以抓个包对比一下你的发出的请求。 如果postman可以,代码不可以。可能是因为postman请求比较干净,代码有框架的影响。

image.png

但是postman里面的md5值不一样 Date: Tue, 07 Nov 2023 09:18:44 GMT Content-Type: application/json Content-MD5: ec9f194f3e742fac4bed527b37a036ff