收藏
回答

签名错误,请检查后再试

调用接口: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": "签名错误,请检查后再试"
}


最后一次编辑于  1天前
回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容