微信平台自动获取Token

来源:互联网 发布:百度一下淘宝男裤 编辑:程序博客网 时间:2024/05/06 15:42

微信公众平台的Token有时间限制,超时后需要重新获取。

import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Properties;import net.sf.json.JSONObject;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * 令牌如果过期重新获取Toekn,否则返回配置文件中的Token * @author wpj */public class WeixinTokenUtil {    private final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";    private final Log logger = LogFactory.getLog(WeixinTokenUtil.class);    /**     * 获取令牌     * @return AccessToken     */    public String getAccessToken() {        String accessToken = "";        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        String filePath = this.getClass().getResource("/") + "";        filePath = filePath.replace("classes/", "weixin.properties");        filePath = filePath.replace("file:/", "");        System.out.println("文件路径:"+filePath);        Properties pro = PropertiesUtil.getProperties(filePath);        String accessTime = pro.getProperty("Access_Token_Time");        if (isTokenOverTime(accessTime)) {            String url = ACCESS_TOKEN_URL + "?grant_type=client_credential&";            String appID = pro.getProperty("AppID");            String secret = pro.getProperty("Secret");            String param = "appid=" + appID + "&secret=" + secret;            JSONObject jasonObject = JSONObject.fromObject(HttpRequestUtil.sendHttpsGet(url, param));            accessToken = jasonObject.get("access_token").toString();            if ("".equals(accessToken)) {                logger.info("获取AccessToken失败:" + jasonObject.get("errmsg").toString());            } else {                pro.setProperty("Access_Token", accessToken);                pro.setProperty("Access_Token_Time", sdf.format(new Date()));                String flag = PropertiesUtil.setProperties(pro, filePath) ? "成功" : "失败";                logger.info("更新本地AccessToken成功" + flag + ":" + accessToken);            }        } else {            accessToken = pro.getProperty("Access_Token");        }        return accessToken;    }    /**     * 验证令牌是否过期     * @param accessTime     * @return      */    private boolean isTokenOverTime(String accessTime) {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Calendar cal = Calendar.getInstance();        long time1 = 0;        long time2 = 0;        try {            cal.setTime(sdf.parse(accessTime));            time1 = cal.getTimeInMillis();            cal.setTime(new Date());            time2 = cal.getTimeInMillis();        } catch (Exception e) {            e.printStackTrace();        }        long between_hours = (time2 - time1) / (1000 * 60);        logger.info("AccessToken时间差:" + between_hours);        return between_hours > 115;    }    public static void main(String[] args) {        WeixinTokenUtil wtu = new WeixinTokenUtil();        System.out.println(wtu.getAccessToken());    }}


原创粉丝点击