import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;
import org.json.JSONException;
import org.json.JSONObject;
public class WeixinClick {
public static void main(String[] args) {
String AccessToken = getAccessToken("自己公共号所申请的AppId", "自己公共号所申请的AppSecret");
/*
* AccessToken有效时间为7200秒 两小时
* 所以,在第一次获取后就两个小时内都不用再获取了。
*/
if(!"".equals(AccessToken)&&AccessToken.length()>10){
String msg = onClick(AccessToken);//access_token是第三方访问api资源的票据
System.out.println(getErrMSG(msg));
/*String msg = getClick(AccessToken);
System.out.println(msg);*/
/*String msg = delCilick(AccessToken);
System.out.println(getErrMSG(msg));*/
}else{
System.out.println(getErrMSG(AccessToken));
}
}
/**
* 设置自定义按钮
* @param ACCESS_TOKEN
* @return
*/
public static String onClick(String ACCESS_TOKEN){
String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+ACCESS_TOKEN;
/**
* 设置菜单
*/
JSONObject json = new JSONObject();
try {
JSONObject json1_1 = new JSONObject();
json1_1.accumulate("type", "click");
json1_1.accumulate("key", "day");
json1_1.accumulate("name", "今日订单");
JSONObject json1_2 = new JSONObject();
json1_2.accumulate("type", "click");
json1_2.accumulate("key", "yesterday");
json1_2.accumulate("name", "昨日订单");
JSONObject json1 = new JSONObject();
json1.accumulate("name", "订单查询");
json1.accumulate("sub_button", json1_1);
json1.accumulate("sub_button", json1_2);
JSONObject json2_1 = new JSONObject();
json2_1.accumulate("name", "订单");
json2_1.accumulate("type", "click");
json2_1.accumulate("key", "order");
JSONObject json2_2 = new JSONObject();
json2_2.accumulate("name", "财务");
json2_2.accumulate("type", "click");
json2_2.accumulate("key", "money");
JSONObject json2 = new JSONObject();
json2.accumulate("sub_button", json2_1);
json2.accumulate("sub_button", json2_2);
json2.accumulate("name", "数据");
JSONObject json3 = new JSONObject();
json3.accumulate("name", "绑定");
json3.accumulate("type", "click");
json3.accumulate("key", "bind");
json.accumulate("button", json1);
json.accumulate("button", json2);
json.accumulate("button", json3);
} catch (JSONException e) {
e.printStackTrace();
}
String responeJsonStr = json.toString();
System.out.println(responeJsonStr);
//强制设置可信任证书
Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
post.setRequestBody(responeJsonStr);
post.getParams().setContentCharset("utf-8");
//发送http请求
String respStr = "";
try {
client.executeMethod(post);
respStr = post.getResponseBodyAsString();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String msg = "";
if(!"".equals(respStr)){
JSONObject jsonMsg = null;
try{
jsonMsg = new JSONObject(respStr);
msg = jsonMsg.getString("errcode");
} catch (JSONException e) {
msg = "-1";
}
}
return msg;
}
/**
* 删除自定义按钮
* @param ACCESS_TOKEN
* @return
*/
public static String delCilick(String ACCESS_TOKEN){
String AccessToken = "";
String url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="+ACCESS_TOKEN;
//强制设置可信任证书
Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
post.getParams().setContentCharset("utf-8");
//发送http请求
String respStr = "";
try {
client.executeMethod(post);
respStr = post.getResponseBodyAsString();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(!"".equals(respStr)){
JSONObject json = null;
try{
json = new JSONObject(respStr);
AccessToken = json.getString("access_token");
} catch (JSONException e) {
try {
AccessToken = json.getString("errcode");
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
return AccessToken;
}
/**
* 获取自定义按钮
* @param ACCESS_TOKEN
* @return
*/
public static String getClick(String ACCESS_TOKEN){
String url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token="+ACCESS_TOKEN;
//强制设置可信任证书
Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
post.getParams().setContentCharset("utf-8");
//发送http请求
String respStr = "";
try {
client.executeMethod(post);
respStr = post.getResponseBodyAsString();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return respStr;
}
/**
* 获取access_token
* @param APPID
* @param APPSECRET
* @return
*/
public static String getAccessToken(String APPID,String APPSECRET){
String AccessToken = "";
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET;
//强制设置可信任证书
Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
post.getParams().setContentCharset("utf-8");
//发送http请求
String respStr = "";
try {
client.executeMethod(post);
respStr = post.getResponseBodyAsString();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(!"".equals(respStr)){
JSONObject json = null;
try{
json = new JSONObject(respStr);
AccessToken = json.getString("access_token");
} catch (JSONException e) {
try {
AccessToken = json.getString("errcode");
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
return AccessToken;
}
/**
* 请求结果认证
* @param code
* @return
*/
public static String getErrMSG(String code){
String returnStr = "";
int c = Integer.valueOf(code);
switch (c) {
case -1:
returnStr = "系统繁忙";
break;
case 0:
returnStr = "请求成功";
break;
case 40001:
returnStr = "验证失败";
break;
case 40002:
returnStr = "不合法的凭证类型";
break;
case 40003:
returnStr = "不合法的OpenID";
break;
case 40004:
returnStr = "不合法的媒体文件类型";
break;
case 40005:
returnStr = "不合法的文件类型";
break;
case 40006:
returnStr = "不合法的文件大小";
break;
case 40007:
returnStr = "不合法的媒体文件id";
break;
case 40008:
returnStr = "不合法的消息类型";
break;
case 40009:
returnStr = "不合法的图片文件大小";
break;
case 40010:
returnStr = "不合法的语音文件大小";
break;
case 40011:
returnStr = "不合法的视频文件大小";
break;
case 40012:
returnStr = "不合法的缩略图文件大小";
break;
case 40013:
returnStr = "不合法的APPID";
break;
case 40014:
returnStr = "不合法的access_tok
没有合适的资源?快使用搜索试试~ 我知道了~
微信公众平台服务号--自定义菜单示例代码
共2个文件
java:2个
需积分: 5 0 下载量 102 浏览量
2022-06-29
16:34:09
上传
评论
收藏 3KB RAR 举报
温馨提示
微信公众平台服务号--自定义菜单示例代码
资源详情
资源评论
资源推荐
收起资源包目录
公众号菜单.rar (2个子文件)
公众号菜单
WeixinClick.java 10KB
MySSLProtocolSocketFactory.java 3KB
共 2 条
- 1
huntlee
- 粉丝: 2
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0