微信公众号开发者模式 定时任务 获取ACCESS_TOKEN

时间:2021-08-26 07:51:44
            Timer timer = new Timer();
myTask = new MyTask();
timer.schedule(myTask, 0, 1000 * 60 * 60);

/***
* 一天有获取2000次的限制 ,设置1小时获取一次AccessToken防止超出请求限制
*
* @author wWX323797
*
*/


public class MyTask extends TimerTask {

public static WechatUtil wechatUtil = new WechatUtil();
public static JSONObject access_token_json;
public static String ACCESS_TOKEN = null;

@Override
public void run() {
// TODO Auto-generated method stub
access_token_json = wechatUtil.getAccess_token();
ACCESS_TOKEN = access_token_json.getString("access_token");
}
}
    public JSONObject getAccess_token() {
try {
URL url = new URL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret=" + appSecret + "");
jsonObject = getFromWechat(url);// 发送http请求到微信端获取返回值

} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}