关于第一次编写第三方接口的心得,编写过程~~
自己遇到的问题:
1、解密(第一次接触,不太会~)
2、BO返回值的构造(属性名需要和接口文档给的属性名一致)
3、授权页面(发起页面和回调页面的域名需要一致)
1、获取验证票据
第三方平台会推送component_verify_ticket但数据是加密的,需要我们进行解密。先可以看到平台提供了一个加密解密的代码,本人用的是Java
以下是平台给的代码
public class Program {
public static void main(String[] args) throws Exception {
//
// 第三方回复公众平台
//
// 需要加密的明文
String encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
String token = "pamtest";
String timestamp = "1409304348";
String nonce = "xxxxxx";
String appId = "wxb11529c136998cb6";
String replyMsg = " 中文1407743423 ";
WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
String mingwen = pc.encryptMsg(replyMsg, timestamp, nonce);
System.out.println("加密后: " + mingwen);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
StringReader sr = new StringReader(mingwen);
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 = " ";
String fromXML = String.format(format, encrypt);
//
// 公众平台发送消息给第三方,第三方处理
//
// 第三方收到公众号平台发送的消息
String result2 = pc.decryptMsg(msgSignature, timestamp, nonce, fromXML);
System.out.println("解密后明文: " + result2);
//pc.verifyUrl(null, null, null, null);
}
}
执行后得到的结果是:
加密后: <xml>
<Encrypt><![CDATA[0VfRjG75jmpsX5/mCTLT/CB4zWs/BvBnZ5VcN6TbE5T3YYcbICOmhw/qbJfal9SVCO9oujsJoQ3/0p1mwuI1RyHosRW3deho6wKYQu6HXjLnKPRtYyGZBBjPxeGeieUVsJeS5hDFwHT/N/sDvIprrVUrU9HQH19SOfP8WT61angqREMm8xdIbTkrbTCTzDTwXtlyow+DsMsLU4Lbu7lpXushwxHCCLryjDsFG5MvEj8axnADv3HwkfNvtOUVk1dGOhoKAX+xj5x2C5/Ph1f5XDZ3UNZq8A+vH2UI/gmTL5Qxv8L1JwrULoAVF8q0UK/fH+V5kCIUSy6/JRPpS6XW6SeGYYBEWNHuADCmZGvm9ORHZlBK3FYIACKyQiqMbJj3kPYrb7ok1/Yo9sRXvCll9Zf/8uzgQonp+UZFEFvNFRX18/aK7WigR4fYrTFtEl+z6VPwJqespfLOzRBd8+K4fSWwhDEjaMLwgngoVKIqpCecB6d/pkFi/4U8F0NiaGd53WvT1AxuQI3sbpXcGdumd7SoDGfuqPzjoP8WgLXo4xGxEyhnzFn7RbNbai8dngQR7bJ4Ak9KWmNZ93zbB35IknvTIUEs0sKMqjXzMoFsNjQcfAXPx8jv/xLOzdm2/dMB]]></Encrypt>
<MsgSignature><![CDATA[cdb10cccd628471b29b4dda418c424b8d8fc3829]]></MsgSignature>
<TimeStamp>1409304348</TimeStamp>
<Nonce><![CDATA[xxxxxx]]></Nonce>
</xml>
解密后明文: 中文<xml><ToUserName><![CDATA[oia2TjjewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>
这里是拿一个解密好数据进行加密再解密,而我们拿到的直接是加密串,所以需要构造成加密后的数据格式拿去解密即可。
2、获取令牌
主要是要保证自己的ip地址在微信开放平台的白名单内。
附一个代码,下面的接口都差不多的~
public static WxComponentAccessTokenBO getComponentAccessToken(String componentAppid, String componentAppsecret) {
WxComponentAccessTokenBO result = null;
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(URL_GET_COMPONENT_ACCESS_TOKEN);
postMethod.setRequestHeader("Content-Type", "application/json");
JSONObject jsonObject = new JSONObject();
String cacheValue = "";
if (StringUtils.isBlank(cacheValue)) {
cacheValue = getComponentVerifyTicket();
}
jsonObject.put("component_appid", componentAppid);
jsonObject.put("component_appsecret", componentAppsecret);
jsonObject.put("component_verify_ticket", cacheValue);
try {
RequestEntity se = new StringRequestEntity(jsonObject.toString(), "application/json", "UTF-8");
postMethod.setRequestEntity(se);
client.executeMethod(postMethod);
// System.out.println("getComponentAccessToken:" + postMethod.getResponseBodyAsString());
result = JSONObject.parseObject(postMethod.getResponseBodyAsString(), WxComponentAccessTokenBO.class);
} catch (Exception e) {
e.printStackTrace();
}
return resu
3、获取预授权码
这里遇到了获取令牌过期了的情况。
4、使用授权码获取授权信息
这里传入的authorization_code不是前面获得的预授权码,而是根据预授权码,第三方平台方 appid,回调 URI可以引入用户进入授权页,扫码后进入到回调页面,上面地址栏就能得到授权码。
ps:发起页面和回调页面的域名需要一致,不能在地址栏直接写地址,需要http请求才行。
主要前面这几个接口写好了,后面都是大同小异的啦~~,第一次接触第三方接口的编写,还是有很多收获的!