生产发版小程序码生成不了,开始以为token任务没有执行,可能token失效了,经查看不是,因为发版内容改动不涉及小程序码生成过程,一直好好的突然就不能用了
追查过程
本地单元测试输出微信接口(
POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
)请求返回值如下:
{"errcode":47001,"errmsg":"data format error rid: 5fbfdd5b-4ebbe607-3b9ece4a"}
跟数据格式有关,查了一下说是参数不是json格式,只看错误提示可能是这个原因,但就是想不通之前的正常的时候小程序码是怎么出来的?原来代码:
Map<String, Object> param = new HashMap<>();
param.put("scene", scene);
param.put("page", page);
param.put("width", 280);
param.put("auto_color", false);
Map<String, Object> line_color = new HashMap<>();
line_color.put("r", 0);
line_color.put("g", 0);
line_color.put("b", 0);
param.put("line_color", line_color);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
inputStream = new ByteArrayInputStream(restTemplate.exchange(url, HttpMethod.POST, new HttpEntity(param, headers), byte[].class).getBody());
同样的代码参数格式就突然变了,总而言之难以信服,但眼下又没有别的办法,只能重新换种请求方法
// param还是上面的param
byte[] bytes = HttpUtil.doPost(url, JSON.toJSONString(param));
public static byte[] doPost(String url, String json) throws Exception {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
CloseableHttpResponse response = client.execute(post);
byte[] bytes = EntityUtils.toByteArray(response.getEntity());
response.close();
return bytes;
}
换成这种方式请求正常了,restTemplate是spring-web包下的,跟了一下里面用的sun.net.www.protocol.http.HttpURLConnection,httpclient是apache httpcompoments包下的,里面用的java.net.Socket,先记下研究研究。