先来一张发布成功的截图。
在进行全网发布的时候说一下我遇到的问题,看截图
说一下解决思路:看一下官方全网发布说明:
具体测试步骤如下(微信后台会提前自动将专用测试公众号授权给第三方平台方,并且将会在专用测试公众号自动授权给第三方平台时,推送query_auth_code给服务方),但请注意,如果第三方平台未勾选消息管理权限集,则会省去相应的全网发布检测步骤,包括第2步和第3步。:
1、模拟粉丝触发专用测试公众号的事件,并推送事件消息到专用测试公众号,第三方平台方开发者需要提取推送XML信息中的event值,并在5秒内立即返回按照下述要求组装的文本消息给粉丝。
-
1)微信推送给第三方平台方: 事件XML内容(与普通公众号接收到的信息是一样的)
-
2)服务方开发者在5秒内回应文本消息并最终触达到粉丝:文本消息的XML中Content字段的内容必须组装为:event + “from_callback”(假定event为LOCATION,则Content为: LOCATIONfrom_callback)
2、模拟粉丝发送文本消息给专用测试公众号,第三方平台方需根据文本消息的内容进行相应的响应:
-
1)微信模推送给第三方平台方:文本消息,其中Content字段的内容固定为:TESTCOMPONENT_MSG_TYPE_TEXT
-
2)第三方平台方立马回应文本消息并最终触达粉丝:Content必须固定为:TESTCOMPONENT_MSG_TYPE_TEXT_callback
3、模拟粉丝发送文本消息给专用测试公众号,第三方平台方需在5秒内返回空串表明暂时不回复,然后再立即使用客服消息接口发送消息回复粉丝
-
1)微信模推送给第三方平台方:文本消息,其中Content字段的内容固定为: QUERY_AUTH_CODE:$query_auth_code$(query_auth_code会在专用测试公众号自动授权给第三方平台方时,由微信后台推送给开发者)
-
2)第三方平台方拿到$query_auth_code$的值后,通过接口文档页中的“使用授权码换取公众号的授权信息”API,将$query_auth_code$的值赋值给API所需的参数authorization_code。然后,调用发送客服消息api回复文本消息给粉丝,其中文本消息的content字段设为:$query_auth_code$_from_api(其中$query_auth_code$需要替换成推送过来的query_auth_code)
返回Api文本消息是调用的方法3;但是调用方法3时是不需要进行加密的。
下面是java的代码。
//公众号消息与事件接收URL
@RequestMapping(value = "msg/{APPID}",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String msg(@PathVariable("APPID") String appId, @RequestBody(required = false) String sPostData,
HttpServletRequest req, HttpServletResponse response) throws IOException {
String encodingAesKey = fileUtil.getWeixinEncodingaeskey();
String token = WeiXinSDK.TOKEN;
String component_appid = fileUtil.getWeixinAppid();
String encrypt_type = req.getParameter("encrypt_type");
String msgSignature = req.getParameter("msg_signature");
String nonce = req.getParameter("nonce");
String timestamp = req.getParameter("timestamp");
WXBizMsgCrypt pc =null;
String result2 = null;
try {
pc = new WXBizMsgCrypt(token, encodingAesKey, component_appid);
// 第三方收到公众号平台发送的消息
result2 = pc.decryptMsg(msgSignature, timestamp, nonce, sPostData, "ToUserName");
//将获取到的xml格式的数据装成Map
Document doc;
Map<String,Object> map = new HashMap<String, Object>();
try {
doc = DocumentHelper.parseText(result2);
map = (Map<String, Object>) XmlUtils.xml2map(doc.getRootElement());
} catch (DocumentException e) {
e.printStackTrace();
}
if(map!=null){
map.put("accountSuiteCode", DbContextHolder.getCurrentAccountSuit());
weiXinManageService.saveEvent(map);
System.out.println("解密后消息明文: " + map);
}
if(appId.equals("wx570bc396a51b8ff8")){
String msgType = map.get("MsgType")+"";
String toUserName = map.get("ToUserName")+"";
String fromUserName = map.get("FromUserName")+"";
if(msgType.equals("event")){
String event = map.get("Event")+"";
String a = replyEventMessage(req,response,event,fromUserName,toUserName);
System.out.println(a);
return pc.encryptMsg(a, timestamp, nonce);
}
if(msgType.equals("text")){ //标示文本消息,
String content = map.get("Content")+"";
String s = processTextMessage(req,response,content,fromUserName,toUserName);//用文本消息去拼接字符串。微信规定
System.out.println(s);
if(s!=null)return pc.encryptMsg(s, timestamp, nonce);
else return "";
}
}
} catch (AesException e) {
e.printStackTrace();
}
return "";
}
private String processTextMessage(HttpServletRequest req,
HttpServletResponse response, String content,
String fromUserName, String toUserName) throws IOException {
if("TESTCOMPONENT_MSG_TYPE_TEXT".equals(content)){
String returnContent = content+"_callback";
return replyTextMessage(req, response, returnContent, toUserName, fromUserName);
}else if(StringUtils.startsWithIgnoreCase(content, "QUERY_AUTH_CODE")){
String authorization_code = content.split(":")[1];
weixinSDK.getQueryAuth(authorization_code);
replyApiTextMessage(authorization_code, fromUserName);
return null;
}
return null;
}
private void replyApiTextMessage(String auth_code, String fromUserName) {
Map<String,Object> result = new HashMap<String, Object>();
result.put("touser",fromUserName);
result.put("msgtype", "text");
Map<String,Object> text = new HashMap<String, Object>();
text.put("content", auth_code+"_from_api");
result.put("text", text);
weixinSDK.sendCustomMessage(result);
}
private String replyEventMessage(HttpServletRequest req,
HttpServletResponse response, String event,
String fromUserName, String toUserName) {
String content = event + "from_callback";
return replyTextMessage(req,response,content,toUserName,fromUserName);
}
private String replyTextMessage(HttpServletRequest req,
HttpServletResponse response, String content,
String toUserName, String fromUserName) {
Long createTime = System.currentTimeMillis() / 1000;
StringBuffer sb = new StringBuffer();
sb.append("<xml>");
sb.append("<ToUserName><![CDATA["+fromUserName+"]]></ToUserName>");
sb.append("<FromUserName><![CDATA["+toUserName+"]]></FromUserName>");
sb.append("<CreateTime>"+createTime.toString()+"</CreateTime>");
sb.append("<MsgType><![CDATA[text]]></MsgType>");
sb.append("<Content><![CDATA["+content+"]]></Content>");
sb.append("</xml>");
return sb.toString();
}
@SuppressWarnings("unchecked")
public Map<String,Object> sendCustomMessage(Map<String, Object> result){
String apiMethod = "message/custom/send";
result.put("accountSuiteCode", "jwtest");
//先从内存缓存中取出保存的access_token
String access_token = null;
try {
access_token = getAuthorizerToken();
} catch (Exception e) {
result.put("error", String.format("调用微信接口 %s 时,获取微信access_token出错:%s", apiMethod, ExceptionUtils.getRootCauseMessage(e)));
return result;
}
// if(result.get("error")!=null){
// try {
// access_token = WeiXinTokenService.getToken();
// } catch (Exception e) {
// result.put("error", String.format("调用微信接口 %s 时,获取微信access_token出错:%s", apiMethod, ExceptionUtils.getRootCauseMessage(e)));
// return result;
// }
// }
HttpPost httppost = new HttpPost(String.format("%s%s?access_token=%s", API_URL, apiMethod, access_token));
try {
StringEntity s = new StringEntity(new Gson().toJson(result));
s.setContentEncoding("UTF-8");
httppost.setEntity(s);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
result = GsonTools.getObject(
EntityUtils.toString(response.getEntity(), "UTF-8"), Map.class);
if(result==null)throw new Exception("...");
if(result.get("errcode")!=null && NumberUtil.getDoubleValue(result,"errcode")!=0){
throw new Exception(result.get("errmsg")+"");
}
}else{
result.put("error", String.format("调用微信接口 %s 出错:HTTP状态:%d 描述:%s ",apiMethod, response.getStatusLine().getStatusCode(),response.getStatusLine().getReasonPhrase()));
}
} catch (Exception e) {
result.put("error", String.format("调用微信接口 %s 出错:%s ",apiMethod,ExceptionUtils.getRootCauseMessage(e)));
}
return result;
}
这是在接受消息时进行处理的代码。原文是http://blog.csdn.net/duxing_langzi/article/details/48266391,我根据自己的需求改动了一些,调用方法1和2时只需要将Content内容替换掉要求的内容即可,然后加密return 加密的内容回去即可
方法3的使用则需要Token全网发布的时候会模拟推送component_verify_ticket给开发者,我们在接受到component_verify_ticket换区token即可