开始对于component_verify_ticket这个参数我一直没弄明白,所以使用这种post请求方式获取第三方平台的access_token就没能成功。
经过仔细阅读文档后解决了这一问题:
----------------------------------------------------------------------官方文档---开始---------------------------------------------------------------
8、推送component_verify_ticket协议
在公众号第三方平台创建审核通过后,微信服务器会向其“授权事件接收URL”每隔10分钟定时推送component_verify_ticket。第三方平台方在收到ticket推送后也需进行解密(详细请见【消息加解密接入指引】),接收到后必须直接返回字符串success。
POST数据示例
<xml>
<AppId> </AppId>
<CreateTime>1413192605 </CreateTime>
<InfoType> </InfoType>
<ComponentVerifyTicket> </ComponentVerifyTicket>
</xml>
字段说明
字段名称 | 字段描述 |
---|---|
Appid | 第三方平台appid |
CreateTime | 时间戳 |
InfoType | component_verify_ticket |
ComponentVerifyTicket | Ticket内容 |
----------------------------------------------------------------------官方文档----结束--------------------------------------------------------------
/**
* 在公众号第三方平台创建审核通过后,微信服务器会向其“授权事件接收URL”每隔10分钟定时推送component_verify_ticket。
* 第三方平台方在收到ticket推送后也需进行解密(详细请见【消息加解密接入指引】),接收到后必须直接返回字符串success。
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String msgSignature = request.getParameter("msg_signature");
String timeStamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String token = "yangchao";
String encodingAesKey = "*****";
String appId = "*********";
/*
* byte[] data = Request.BinaryRead(Request.TotalBytes);
*
* string postData * = Encoding.Default.GetString(data);
*/
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
StringBuffer sb = new StringBuffer();
String line = null;
while ((line=br.readLine()) != null) {
sb = sb.append(line);
}
String postData = sb.toString();
System.out.println(msgSignature+"------"+ timeStamp+"------"+ nonce+"------"+ postData);
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
StringReader sr = new StringReader(postData);
InputSource is = new InputSource(sr);
Document document = db.parse(is);
Element root = document.getDocumentElement();
NodeList nodelist1 = root.getElementsByTagName("Encrypt");
NodeList nodelist2 = root.getElementsByTagName("MsgSignature");
String encrypt = nodelist1.item(0).getTextContent();
// String msgSignature = nodelist2.item(0).getTextContent();
String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
String fromXML = String.format(format, encrypt);
/**
* 构造函数
*
* @param token
* 公众平台上,开发者设置的token
* @param encodingAesKey
* 公众平台上,开发者设置的EncodingAESKey
* @param appId
* 公众平台appid
*
* @throws AesException
* 执行失败,请查看该异常的错误码和具体的错误信息
*/
WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
String result = pc.decryptMsg(msgSignature, timeStamp, nonce, fromXML);
System.out.println(result + ".................");
Component_verify_ticket c = new Component_verify_ticket();
c = c.getComponent_verify_ticket(result);
System.out.println(c.getComponentVerifyTicket()+"...............");
} catch (Exception e) {
e.printStackTrace();
}
// 响应消息
PrintWriter out = response.getWriter();
out.print("success");
}
--------------------------------------------------------
/**
* 8、推送component_verify_ticket协议
*
*
* 在公众号第三方平台创建审核通过后,微信服务器会向其“授权事件接收URL”每隔10分钟定时推送component_verify_ticket。
* 第三方平台方在收到ticket推送后也需进行解密(详细请见【消息加解密接入指引】),接收到后必须直接返回字符串success。
*
* @author YangChao
*
*/
public class Component_verify_ticket {
// 第三方平台appid
private String AppId;
// 时间戳
private String CreateTime;
// component_verify_ticket
private String InfoType;
// Ticket内容
private String ComponentVerifyTicket;
// 获取其对应的xml数据串
public String getXML() {
XStream xStream = new XStream(new DomDriver());
xStream.alias("xml", this.getClass());
// 设置成员的别名
xStream.aliasField("AppId", this.getClass(), "AppId");
xStream.aliasField("CreateTime", this.getClass(), "CreateTime");
xStream.aliasField("InfoType", this.getClass(), "InfoType");
xStream.aliasField("ComponentVerifyTicket", this.getClass(), "ComponentVerifyTicket");
return xStream.toXML(this);
}
/**
* 根据xml数据获取Component_verify_ticket对象
* @param xml
* @return
*/
public Component_verify_ticket getComponent_verify_ticket(String xml){
XStream xStream = new XStream(new DomDriver());
xStream.alias("xml", this.getClass());
// 设置成员的别名
xStream.aliasField("AppId", this.getClass(), "AppId");
xStream.aliasField("CreateTime", this.getClass(), "CreateTime");
xStream.aliasField("InfoType", this.getClass(), "InfoType");
xStream.aliasField("ComponentVerifyTicket", this.getClass(), "ComponentVerifyTicket");
Component_verify_ticket component_verify_ticket = (Component_verify_ticket) xStream.fromXML(xml);
return component_verify_ticket;
}