Micro message public platform custom menu created code to achieve java version

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Make a custom menu two days, finally settled, now share experience, so that the subsequent few detours......

Well, look at the first official micro message API

The official written in great detail, but after I have read very confused, don't know what it feels like you.   I know that post is a parameter of the request to the URL, but the specific how to send., To make a JSP page, Use <form> to send, But for various reasons., So the change of ideas, Then there is the Java get or post to access the URL idea, After a run., Prompts the “ 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” this error, The query is the issue of a certificate, In the online struggle, Later, a word reminded me (your URL is not a public network, Service access success., Can your development environment can not), I was new in their own computer engineering, Not deployed to the network, You give Tencent post request., But Tencent access not to your local project, So it appears that mistake.

So the following conclusion: the deployment of its own new works to the network, is the application of your network, to achieve this function. Do not know, refer to my previous article “ public use micro message platform to realize the automatic reply message — — Java”.

Go to (I to Baidu cloud Developer Center as an example):

The application of their deployment in Baidu cloud Developer Center in index.jsp is amended as follows:

 1 <%@page import="java.io.*"%>
 2 <%@page import="java.net.*" %>
 3 <%@page import="org.json.*" %>
 4 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
 5 
 6 <%
 7   final String appId = " ";   
 8   final String appSecret = " ";  //Their APPIP and APPSECRET
 9   
10  %>
11 <%
12 class TestGetPost{
13 
14     public String getAccess_token(){  // ACCESS_TOKEN
15       
16       String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId + "&secret=" +appSecret;
17      
18       String accessToken = null;
19      try {
20             URL urlGet = new URL(url);
21             HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();    
22              
23             http.setRequestMethod("GET");      //Must be get mode request    
24             http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");    
25             http.setDoOutput(true);        
26             http.setDoInput(true);
27             System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//Connection timeout of 30 seconds
28             System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //Read the timeout of 30 seconds
29 
30             http.connect();
31          
32             InputStream is =http.getInputStream();
33             int size =is.available();
34             byte[] 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             }
45        return accessToken;
46     }
47     public int createMenu() throws IOException {
48          String user_define_menu = "{\"button\":[{\ "type\ \" click\ "",\"name\":\"Project management \",\"Key\": "20_PROMANAGE\"},{\ "type\ \" click\ "",\"name\":\"Operation of the agency \",\"Key\": "30_ORGANIZATION\"},{\"name\":\"The daily work.",\"Sub_button\": "type\" [{\: \ "click\",\"name\":\"To do the job.",\"Key\": "01_WAITING\"},{\ "type\ \" click\ "",\"name\":\"Do the job.",\"Key\": "02_FINISH\"},{\ "type\ \" click\ "",\"name\":\"I am single,",\"Key\": "03_MYJOB\"},{\ "type\ \" click\ "",\"name\":\"Notice the message box.",\"Key\": "04_MESSAGEBOX\"},{\"type\":\"click\",\"name\":\"Sign in\",\"key\":\"05_SIGN\"}]}]}";
49          //Here I want to structure body, replaced
50          String access_token= getAccess_token();
51 
52          String action = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token;
53          try {
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("sun.net.client.defaultConnectTimeout", "30000");//Connection timeout of 30 seconds
62             System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //Read the timeout of 30 seconds
63 
64             http.connect();
65             OutputStream os= http.getOutputStream();    
66             os.write(user_define_menu.getBytes("UTF-8"));//The incoming parameters    
67             os.flush();
68             os.close();
69          
70             InputStream is =http.getInputStream();
71             int size =is.available();
72             byte[] 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             }    
81         return 0;
82     }
83 }%>
84 <%
85     TestGetPost tgp = new TestGetPost();
86     
87     tgp.createMenu();
88  %>

index.jsp

After deployment, input current domain name can be Baidu cloud Developer Center changed application directly in the browser address bar, and then focus on micro message public accounts (see effect has been concern, unfollow and re focus, to see the effect of).

Remember in the application in the XML file

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

As the effect is created:


Tips:

1,View the custom menu: input directly in the browser address bar

https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN  (Into your own access_token, access_token is effective in a period of time, so get in the paste, this is no problem)

2,Delete a custom menu:

https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN (Remove and check again)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Mick at November 19, 2013 - 7:06 AM