public class GetAccessToken {
public final static String app_id = GlobalKeys.getProperties("app_id");
public final static String app_secret = GlobalKeys.getProperties("app_secret");
public final static String url = "https://api.weixin.qq.com/cgi-bin/token";
public static boolean getAccessToken(){
String result = HttpUtil.sendGet(url, "grant_type=client_credential&appid="+app_id+"&secret="+app_secret);
if(StringUtils.isEmpty(result)){
return false;
}
try{
JSONObject js = JSONObject.fromObject(result);
String at = js.get("access_token").toString();
if(StringUtils.isEmpty(at)){
return false;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd:hh-mm-ss.SSS");
WxAs wxAs = new WxAs(at,dateFormat.format(new Date()) );
GlobalCache.as=at;
return GlobalCache.getWxDao().update(wxAs);
}catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static String getQr(){
JSONObject js = new JSONObject();
js.put("expire_seconds", 604800);
js.put("action_name", "QR_SCENE");
JSONObject j1 = new JSONObject();
j1.put("scene_id", 111111);
JSONObject j2 = new JSONObject();
j2.put("scene", j1);
js.put("action_info", j2);
String s = HttpUtil.sendPost("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+GlobalCache.as, js.toString());
try{
JSONObject rs = JSONObject.fromObject(s);
String url = rs.getString("url");
if(StringUtils.isEmpty(url)){
return null;
}
return url;
}catch(Exception e){
return null;
}
}
}
请自行替换appid等静态字符串