微信公众平台自定义菜单创建代码实现—java版
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
微信公众平台⾃定义菜单创建代码实现—java版
搞了两天的⾃定义菜单,终于搞定了,现在分享下⼼得,以便后来者少⾛弯路......
好了,先看先微信官⽅的API
官⽅写的很详细,但是我看完后很茫然,不知道你们什么感觉。
我知道是post⼀个带参数的请求给url,可是具体怎么发送呢,开始想做⼀个jsp页⾯,使⽤<form>来发送,可是种
种原因不⾏,所以换种想法,于是有了java get或post访问url的想法,弄好后⼀运⾏,会提⽰“.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:
PKIX path validation failed: java.security.cert.CertPathValidatorException: basic constraints check failed: pathLenConstraint violated - this cert must be the last cert in the
certification path”这种错误,查询是证书的问题,在⽹上百般折腾,后来⾼⼈⼀句话提醒我了(你的url不是公⽹,服务接⼊成功了,可以你的开发环境不能),我是在⾃⼰电脑
上新建的⼯程,没有部署到⽹络上,你给腾讯发post请求了,可是腾讯接⼊不到你的本机⼯程上,所以会出现那个错误了。
所以有了下⾯的结论:把⾃⼰新建的⼯程部署到⽹络上,就是你⽹络的应⽤上,才能实现这⼀功能。
不懂的者,参考下我以前的⽂章“利⽤微信公众平台实现⾃动回复消息——
java版”。
进⼊正题(我以百度云开发者中⼼为例):
将⾃⼰在百度云开发者中⼼中部署的应⽤中的index.jsp修改如下:
1 <%@page import="java.io.*"%>
2 <%@page import=".*" %>
3 <%@page import="org.json.*" %>
4 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
5
6 <%
7final String appId = " ";
8final String appSecret = " "; //⾃⼰的APPIP 和APPSECRET
9
10 %>
11 <%
12class TestGetPost{
13
14public String getAccess_token(){ // 获得ACCESS_TOKEN
15
16 String url = "https:///cgi-bin/token?grant_type=client_credential&appid="+ appId + "&secret=" +appSecret;
17
18 String accessToken = null;
19try {
20 URL urlGet = new URL(url);
21 HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
22
23 http.setRequestMethod("GET"); //必须是get⽅式请求
24 http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
25 http.setDoOutput(true);
26 http.setDoInput(true);
27 System.setProperty(".client.defaultConnectTimeout", "30000");//连接超时30秒
28 System.setProperty(".client.defaultReadTimeout", "30000"); //读取超时30秒
29
30 http.connect();
31
32 InputStream is =http.getInputStream();
33int size =is.available();
34byte[] jsonBytes =new byte[size];
35 is.read(jsonBytes);
36 String message=new String(jsonBytes,"UTF-8");
37
38 JSONObject demoJson = new JSONObject(message);
39 accessToken = demoJson.getString("access_token");
40
41 System.out.println(message);
42 } catch (Exception e) {
43 e.printStackTrace();
44 }
45return accessToken;
46 }
47public int createMenu() throws IOException {
48 String user_define_menu = "{\"button\":[{\"type\":\"click\",\"name\":\"项⽬管理\",\"key\":\"20_PROMANAGE\"},{\"type\":\"click\",\"name\":\"机构运作\",\"key\":\"30_ORGANIZATION\"},{\"name\":\"⽇常⼯作\",\"sub_button\":[{\"type\":\"click 49//此处改为⾃⼰想要的结构体,替换即可
50 String access_token= getAccess_token();
51
52 String action = "https:///cgi-bin/menu/create?access_token="+access_token;
53try {
54 URL url = new URL(action);
55 HttpURLConnection http = (HttpURLConnection) url.openConnection();
56
57 http.setRequestMethod("POST");
58 http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
59 http.setDoOutput(true);
60 http.setDoInput(true);
61 System.setProperty(".client.defaultConnectTimeout", "30000");//连接超时30秒
62 System.setProperty(".client.defaultReadTimeout", "30000"); //读取超时30秒
63
64 http.connect();
65 OutputStream os= http.getOutputStream();
66 os.write(user_define_menu.getBytes("UTF-8"));//传⼊参数
67 os.flush();
68 os.close();
69
70 InputStream is =http.getInputStream();
71int size =is.available();
72byte[] jsonBytes =new byte[size];
73 is.read(jsonBytes);
74 String message=new String(jsonBytes,"UTF-8");
75 System.out.println(message);
76 } catch (MalformedURLException e) {
77 e.printStackTrace();
78 } catch (IOException e) {
79 e.printStackTrace();
80 }
81return 0;
82 }
83 }%>
84 <%
85 TestGetPost tgp = new TestGetPost();
86
87 tgp.createMenu();
88 %>
index.jsp
部署好后,直接在浏览器地址栏中输⼊⾃⼰百度云开发者中⼼下改应⽤的当前域名即可,然后关注微信公众账号看效果(已关注的,取消关注并重新关注,即可看到效果)。
记得将应⽤中的xml⽂件⾥的
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
创建后效果为:
⼩提⽰:
1、⾃定义菜单的查看:直接在浏览器地址栏中输⼊
(换成⾃⼰的access_token,access_token在⼀段时间⾥是有效的,所以获得后,在粘贴到此是没有问题的)2、⾃定义菜单的删除:
(删除与查看同理)。