String code;
String appid;
String secret;
HttpResponse response = HttpRequest.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + secret + "").execute();
JSONObject jsonObject = new JSONObject(response.body());
if(!jsonObject.containsKey("access_token")){
throw new BusinessException("获取微信令牌失败");
}
String accessToken = jsonObject.getStr("access_token");
JSONObject jsonCode = new JSONObject();
jsonCode.putOpt("code",code);
String resPhone = HttpUtil.post("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken, jsonCode.toString());
if(StringUtil.isEmpty(resPhone)){
throw new BusinessException("请求phone失败");
}
JSONObject phoneJson = new JSONObject(resPhone);
JSONObject phoneObj = phoneJson.getJSONObject("phone_info");
log.info("获取phone{}", resPhone);
if(phoneObj == null){
throw new BusinessException("获取phone失败");
}
String phoneNumber = phoneObj.getStr("phoneNumber");
首先要先去微信端接口带上appId和secret获取令牌,拿到令牌之后再去请求获取点好号码的接口,把令牌拼接在url,code需要放在请求体上,code为key,具体code为value,注意一定要是json格式的请求体不然会报格式异常,转json的包是用的huool的工具包
版权声明:本文为weixin_57564298原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。