收藏
回答

调用生成了平台证书接口,报错: 商户证书序列号有误。请使用签名私钥匹配的证书序列号

现在要调用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();
        }
    }
}





回答关注问题邀请回答
收藏

1 个回答

  • Memory
    Memory
    2023-06-19

    用这个脚本验证一下证书和序列号是不是匹配https://github.com/wechatpay-apiv3/wechatpay-postman-script

    2023-06-19
    有用 1
    回复 3
    • A-王文广
      A-王文广
      2023-06-21
      用postman apiv3 还是报一样的异常,微信后台能不能删掉所有的证书重新申请
      2023-06-21
      回复
    • Memory
      Memory
      2023-06-25回复A-王文广
      一年就那么10次,超过就不行了
      2023-06-25
      回复
    • A-王文广
      A-王文广
      2023-07-03回复Memory
      已解决,商户号给错了
      2023-07-03
      回复
登录 后发表内容