现在要调用https://api.mch.weixin.qq.com/v3/certificates 接口生成平台证书,商户证书已申请,为了验证错误又生成了两次,还是报错,用的微信官网wechatpay-apache-httpclient 的测试用例还是不行,报错 ERROR com.wechat.pay.contrib.apache.httpclient.cert.CertificatesManager - Auto update cert failed, statusCode = 401, body = {"code":"SIGN_ERROR","message":"商户证书序列号有误。请使用签名私钥匹配的证书序列号"},
请各位大佬给点指导意见!
用的wechatpay-apache-httpclient里面的示例
public class CertificatesManagerTest {
// 你的商户私钥
private static final String privateKey = ApiclientConstants.APICLIENT_KEY_PEM_0523;//私钥20230523
private static final String merchantId = "163XXXX298"; // 商户号(服务商)
private static final String merchantSerialNumber = "5D26637F16B91XXXXXXXXXXX1B5C0DC6A2AF2"; // 商户证书序列号
private static final String apiV3Key = "4c878c850dXXXXXXXXXX6537f441b569"; // API V3密钥
private CloseableHttpClient httpClient;
CertificatesManager certificatesManager;
Verifier verifier;
private static final HttpHost proxy = null;
@Before
public void setup() throws Exception {
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKey);
// 获取证书管理器实例
certificatesManager = CertificatesManager.getInstance();
// 添加代理服务器
certificatesManager.setProxy(proxy);
// 向证书管理器增加需要自动更新平台证书的商户信息
certificatesManager.putMerchant(merchantId, new WechatPay2Credentials(merchantId,
new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),
apiV3Key.getBytes(StandardCharsets.UTF_8));
// 从证书管理器中获取verifier
verifier = certificatesManager.getVerifier(merchantId);
// 构造httpclient
httpClient = WechatPayHttpClientBuilder.create()
.withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier))
.build();
}
@After
public void after() throws IOException {
httpClient.close();
}
@Test
public void getCertificateTest() throws Exception {
//https://api.mch.weixin.qq.com/v3/certificates
URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/certificates");
HttpGet httpGet = new HttpGet(uriBuilder.build());
httpGet.addHeader(ACCEPT, APPLICATION_JSON.toString());
CloseableHttpResponse response = httpClient.execute(httpGet);
assertEquals(SC_OK, response.getStatusLine().getStatusCode());
try {
HttpEntity entity = response.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity);
} finally {
response.close();
}
}
}
用这个脚本验证一下证书和序列号是不是匹配https://github.com/wechatpay-apiv3/wechatpay-postman-script