public static JSONObject getPhoneNumber(String appId, String secret, String code) {
String tokenUrl = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appId, secret);
com.alibaba.fastjson.JSONObject token = JSON.parseObject(HttpUtil.get(tokenUrl));
String url = "https://api.weixin.qq.com/wxa/business/
?access_token=" + token.getString("access_token");
Map paramMap = new HashMap<>();
paramMap.put("code", code);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity> httpEntity = new HttpEntity<>(paramMap,headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity response = restTemplate.postForEntity(url, httpEntity, Object.class);
return new JSONObject(response.getBody());
}
1:code过期
2:code重复使用
3:code错误
4:检查下是否code和appid不匹配,也就是这个code不是这个appid生成的。