1天前小编发现微信小程序用户:【撑到最后,才有然后】提出了一个关于“Http头Authorization值格式错误,请参考《微信支付商户REST API签名规则》?”的问题,在问题中开发者撑到最后,才有然后对该问题做了如下阐述:
按照官方文档https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_0.shtml的步骤, 获取签名时,依然报 ‘Http头Authorization值格式错误,请参考《微信支付商户REST API签名规则》’。但是我觉得最离谱的是,把官网的命令复制下来执行后,依然报此错误,按理说是报数据错误,各非格式问题。有人员能解答一下吗?
该图片引用微信开放社区
以下是获取签名信息的代码,同样也是官方给的示例代码:
public static void main(String[]args) throws Exception {
//认证类型
String schema = “WECHATPAY2-SHA256-RSA2048”;
String httpurl = “/v3/certificates”;
String httpAuthorization = getAuthorization(“GET”, httpurl, “”);
String curl = “curl https://api.mch.weixin.qq.com/v3/certificates -H ‘Authorization: WECHATPAY2-SHA256-RSA2048 “ + httpAuthorization + “‘”;
System.out.println(curl);
}
/**
* 得到签名信息
* @param method
* @param url
* @param body
* @return
* @throws Exception
*/
public static String getAuthorization(String method, String url, String body) throws Exception {
String nonceStr = StrUtil.generateNonceStr();
long timestamp = System.currentTimeMillis() / 1000;
//获取签名串
String message = buildMessage(method, url, timestamp, nonceStr, body);
System.out.println(“签名串:”+message);
//得到签名
String signature = sign(message.getBytes(“utf-8”));
System.out.println(“签名值:”+signature);
//五项签名信息,无顺序要求
String httpAuthorization = “mchid=”” + MCHID_JDKJ + “”,”
+ “nonce_str=”” + nonceStr + “”,”
+ “signature=”” + signature + “”,”
+ “timestamp=”” + timestamp + “”,”
+ “serial_no=”” + MCHSERIALNO_JDKJ + “””
;
System.out.println(“httpAuthorization:” + httpAuthorization);
return httpAuthorization;
}
/**
* 获取签名值
* @param message
* @return
* @throws Exception
*/
public static String sign(byte[] message) throws Exception {
Signature sign = Signature.getInstance(“SHA256withRSA”);
//# 示例:私钥存储在文件
// PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new FileInputStream(“D:testapiclient_key.pem”));
PrivateKey merchantPrivateKey = getPrivateKey(“D:testapiclient_key.pem”);
sign.initSign(merchantPrivateKey);
sign.update(message);
return Base64.getEncoder().encodeToString(sign.sign());
}
/**
* 获取私钥。
* @param filename 私钥文件路径 (required)
* @return 私钥对象
*/
public static PrivateKey getPrivateKey(String filename) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(filename)), “utf-8”);
try {
String privateKey = content.replace(“—–BEGIN PRIVATE KEY—–“, “”)
.replace(“—–END PRIVATE KEY—–“, “”)
.replaceAll(“s+”, “”);
KeyFactory kf = KeyFactory.getInstance(“RSA”);
return kf.generatePrivate(
new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(“当前Java环境不支持RSA”, e);
} catch (InvalidKeySpecException e) {
throw new RuntimeException(“无效的密钥格式”);
}
}
/**
* 构造签名串
* 签名串一共有五行,每一行为一个参数。行尾以
(换行符,ASCII编码值为0x0A)结束,包括最后一行。如果参数本身以
结束,也需要附加一个* HTTP请求方法
* URL
* 请求时间戳
* 请求随机串
* 请求报文主体
* @param method
* @param url
* @param timestamp
* @param nonceStr
* @param body
* @return
* @throws Exception
*/
public static String buildMessage(String method, String url, long timestamp, String nonceStr, String body) {
return method + ”
“
+ url + ”
“
+ timestamp + ”
“
+ nonceStr + ”
“
+ body + ”
“;
}最后生成的签名信息如下:
该图片引用微信开放社区
最后使用命令执行结果如下:
该图片引用微信开放社区
请问这是哪里的问题
注:以上是微信小程序开发者:”撑到最后,才有然后”对于本问题的一些阐述,这里做一个引用,我们将实时关注微信小程序助手对该问题提出的解决方案。
【4小时前】微信小程序技术运营专员对用户撑到最后,才有然后提出的Http头Authorization值格式错误,请参考《微信支付商户REST API签名规则》?给予如下回复
谢邀:建议用官方的jar包,https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient Central源就能安装到。。。。 https://search.maven.org/artifact/com.github.wechatpay-apiv3/wechatpay-apache-httpclient
微信官方智能小程序的月活用户超过1.5亿,现在有着40多个流量入口,未来估计能够达到70多个。微信官方小程序不仅可以运行在微信官方系列的产品上,像例如爱奇艺、携程等,都是支持微信官方小程序运行的,这个数量高达40多个。像这样情况,对企业来说,这将意味着接触到更多的用户,微信官方的小程序的开放申请就又为企业在新移动时代的网络营销多了一个选择和流量阵地。
如果您也在使用微信小程序,请关注我们,如果您有关于微信小程序的使用问题请关注我们,如果您要找百度小程序模板、微信小程序模板请关注小程序模板网或者去微信开放社区找官方人员给予解决,相信微信官方在小程序上的发力会是一个里程碑!