收藏
回答

签名错误,请检查后再试

调用接口:https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi

1、部分代码:
String token = getToken("POST", httpurl, rootNode.toString());
//请求URL
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");
StringEntity entity = new StringEntity(rootNode.toString(), "utf-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Authorization", schema + " " + token);

CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//完成签名并执行请求
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == 200) {
        System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
    } else if (statusCode == 204) {
        System.out.println("success");
    } else {
        System.out.println("failed,resp code = " + statusCode + ",return body = " + EntityUtils.toString(response.getEntity()));
        throw new IOException("request failed");
    }
} finally {
    response.close();
    httpClient.close();
}

2、token的获取方式(官方代码):
String getToken(String method, HttpUrl url, String body) throws IOException, NoSuchAlgorithmException, SignatureException, InvalidKeyException {
    String nonceStr = UUID.randomUUID().toString().replace("-", "");
    long timestamp = System.currentTimeMillis() / 1000;
    String message = buildMessage(method, url, timestamp, nonceStr, body);
    String signature = sign(message.getBytes("utf-8"));
    
    return "mchid=\"" + mchId + "\","
            + "nonce_str=\"" + nonceStr + "\","
            + "timestamp=\"" + timestamp + "\","
            + "serial_no=\"" + serialNo2 + "\","
            + "signature=\"" + signature + "\"";
}



3、报如下错误:
{
  "code": "SIGN_ERROR",
  "detail": {
    "detail": {
      "issue": "sign not match"
    },
    "field": "signature",
    "location": "authorization",
    "sign_information": {
      "method": "POST",
      "sign_message_length": 311,
      "truncated_sign_message": "POST\n/v3/pay/transactions/jsapi\n1730605701\nc34438f0221f4cd8800e25cb237ba36e\n{\"mchid\"\n    },
    "url": "/v3/pay/transactions/jsapi"
  },
  "message": "签名错误,请检查后再试"
}


最后一次编辑于  11-03
回答关注问题邀请回答
收藏

1 个回答

  • Memory
    Memory
    11-03

    建议直接使用sdk https://pay.weixin.qq.com/docs/merchant/sdk-tools/sdk-tool.html

    11-03
    有用 1
    回复 5
    • 人生路上
      人生路上
      11-03
      感谢感谢,还真是直接请求通了。建议官方文档把文档优化一下,搞个推荐的在哪里,目前发现这一个文档哪一个文档的!
      11-03
      回复
    • Memory
      Memory
      11-03回复人生路上
      有意见或者建议可以发帖说明,会反馈到对应负责人那边的
      11-03
      回复
    • 人生路上
      人生路上
      12-01
      你好,本地没问题,发布到服务器上报这个java.security.InvalidKeyException: Illegal key size  ,和环境有关系吗
      12-01
      回复
    • Memory
      Memory
      12-01回复人生路上
      12-01
      1
      回复
    • 人生路上
      人生路上
      12-01回复Memory
      搞定,谢谢大佬!
      12-01
      回复
登录 后发表内容