微信公眾平台自定義菜單創建代碼實現—java版
搞了兩天的自定義菜單,終於搞定了,現在分享下心得,以便后來者少走彎路......
好了,先看先微信官方的API
官方寫的很詳細,但是我看完后很茫然,不知道你們什么感覺。 我知道是post一個帶參數的請求給url,可是具體怎么發送呢,開始想做一個jsp頁面,使用<form>來發送,可是種種原因不行,所以換種想法,於是有了java get或post訪問url的想法,弄好后一運行,會提示“javax.net.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修改如下:

<%@page import="java.io.*"%>
<%@page import="java.net.*" %>
<%@page import="org.json.*" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
final String appId = " ";
final String appSecret = " "; //自己的APPIP 和APPSECRET
%>
<%
class TestGetPost{
public String getAccess_token(){ // 獲得ACCESS_TOKEN
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId + "&secret=" +appSecret;
String accessToken = null;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); //必須是get方式請求
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//連接超時30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時30秒
http.connect();
InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
JSONObject demoJson = new JSONObject(message);
accessToken = demoJson.getString("access_token");
System.out.println(message);
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
public int createMenu() throws IOException {
String user_define_menu = "{\"button\":[{\"type\":\"click\",\"name\":\"項目管理\",\"key\":\"20_PROMANAGE\"},{\"type\":\"click\",\"name\":\"機構運作\",\"key\":\"30_ORGANIZATION\"},{\"name\":\"日常工作\",\"sub_button\":[{\"type\":\"click\",\"name\":\"待辦工單\",\"key\":\"01_WAITING\"},{\"type\":\"click\",\"name\":\"已辦工單\",\"key\":\"02_FINISH\"},{\"type\":\"click\",\"name\":\"我的工單\",\"key\":\"03_MYJOB\"},{\"type\":\"click\",\"name\":\"公告消息箱\",\"key\":\"04_MESSAGEBOX\"},{\"type\":\"click\",\"name\":\"簽到\",\"key\":\"05_SIGN\"}]}]}";
//此處改為自己想要的結構體,替換即可
String access_token= getAccess_token();
String action = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token;
try {
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//連接超時30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時30秒
http.connect();
OutputStream os= http.getOutputStream();
os.write(user_define_menu.getBytes("UTF-8"));//傳入參數
os.flush();
os.close();
InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
System.out.println(message);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}%>
<%
TestGetPost tgp = new TestGetPost();
tgp.createMenu();
%>
index.jsp
部署好后,直接在瀏覽器地址欄中輸入自己百度雲開發者中心下改應用的當前域名即可,然后關注微信公眾賬號看效果(已關注的,取消關注並重新關注,即可看到效果)。
記得將應用中的xml文件里的
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
創建后效果為:
小提示:
1、自定義菜單的查看:直接在瀏覽器地址欄中輸入
https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN (換成自己的access_token,access_token在一段時間里是有效的,所以獲得后,在粘貼到此是沒有問題的)
2、自定義菜單的刪除:
https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN (刪除與查看同理)