自定义菜单与调用接口创建菜单
2016-04-06 10:43
429 查看
如果我们不需要调用事件推送,则可使用自定义菜单,如果需要调用事件推送,比如扫描二维码,长按识别二维码,则需要使用服务器配置。
一、自定义菜单
略......
二、调用接口创建菜单
2.1.登录微信公众平台,在基本配置,配置URL,如下图。
这个URL是微信所有事件的统一入口,可以理解为回调函数。
2.2.调用微信接口创建菜单
微信接口调试地址
token是去微信服务器获取的,body是json字符串,也就是你需要创建的菜单。
怎么获取token?
appid,secret在基础配置上就能看到。
2.3 后台实现创建,写好菜单字符串,token.
2.4验证创建菜单是否成功
在浏览器输入:https://api.weixin.qq.com/cgi-bin/menu/get?access_token=Token或者调试输入token,然后就可以了。
toekn的还是通过appid和secret在调试接口处获取。
一、自定义菜单
略......
二、调用接口创建菜单
2.1.登录微信公众平台,在基本配置,配置URL,如下图。
这个URL是微信所有事件的统一入口,可以理解为回调函数。
2.2.调用微信接口创建菜单
微信接口调试地址
token是去微信服务器获取的,body是json字符串,也就是你需要创建的菜单。
怎么获取token?
appid,secret在基础配置上就能看到。
2.3 后台实现创建,写好菜单字符串,token.
public static void createMenu(){ // String appId = "wxd30cf078223d3aca"; String jsonStr = "{\"button\":[{\"type\":\"view\",\"name\":\"首页\",\"url\":\"http://xxxxx.com/project<span style="font-family: Arial, Helvetica, sans-serif;">/</span><span style="font-family: Arial, Helvetica, sans-serif;">action</span><span style="font-family: Arial, Helvetica, sans-serif;">/method.do?appIdxxx\"}]}";</span><span style="font-family: Arial, Helvetica, sans-serif;"> </span> jsonStr = jsonStr.replace("@", "=").replace("#","&"); String accessToken = "jB4r8ldK_4loz3UFrC5GcycTabNw1RPKQddbeDsgzklAU2zkodBTgT3OF_4BAQwsK9keoMQVhwJcMhmEqBsuRSWCHI-pCJm9LLL9xOFTDAcYQMeAAADVR"; String url =MENU_CREATE_URL.replace("ACCESS_TOKEN", accessToken); CommonUtil.httpsRequest(url, "POST", jsonStr); } public static void main(String[] args) { createMenu(); }
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) { JSONObject jsonObject = null; try { // 创建SSLContext对象,并使用我们指定的信任管理器初始化 TrustManager[] tm = { new MyX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, tm, new java.security.SecureRandom()); // 从上述SSLContext对象中得到SSLSocketFactory对象 SSLSocketFactory ssf = sslContext.getSocketFactory(); URL url = new URL(requestUrl); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(ssf); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); // 设置请求方式(GET/POST) conn.setRequestMethod(requestMethod); // 当outputStr不为null时向输出流写数据 if (null != outputStr) { OutputStream outputStream = conn.getOutputStream(); // 注意编码格式 outputStream.write(outputStr.getBytes("UTF-8")); outputStream.close(); } // 从输入流读取返回内容 InputStream inputStream = conn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; StringBuffer buffer = new StringBuffer(); while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } // 释放资源 bufferedReader.close(); inputStreamReader.close(); inputStream.close(); inputStream = null; conn.disconnect(); jsonObject = JSON.parseObject(buffer.toString()); } catch (ConnectException ce) { log.error("连接超时:{}", ce); } catch (Exception e) { log.error("https请求异常:{}", e); } return jsonObject; }
2.4验证创建菜单是否成功
在浏览器输入:https://api.weixin.qq.com/cgi-bin/menu/get?access_token=Token或者调试输入token,然后就可以了。
toekn的还是通过appid和secret在调试接口处获取。
相关文章推荐
- [LintCode] 搜索旋转排序数组 Search in Rotated Sorted Array
- Java中的线程(一)
- 找到了与该请求匹配的多个操作
- Leetcode #35. Search Insert Position 搜寻插入位置 解题报告
- 分布式入门之3:副本控制
- Adding the Test API in The ASP.NET Web API Help Page
- 【追求进步】把数组排成最小的数
- java高级之callable与future
- cannot be opened because the project file cannot be parsed
- 里氏替换原则LSP
- cglib与JDK动态代理适用场景
- ubuntu14.04 libgtk-x11-2.0.so.0
- 《leetCode》:Rectangle Area
- Git提交错误:RPC failed; result=22, HTTP code = 411
- eclipse 错误:Description Resource Path Location Type Target runtime
- Linux设备驱动开发详解总结(二)之并发与竞争
- GPU&VS2012&CUDA&matlab&Arrayfire杂记(三)——cuda
- PHP的启动与终止
- (2016春) 作业5:需求分析
- C# Excel操作类