public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { ApiConfig ac = new ApiConfig(); ac.setAppId("wx385e1fe5663a7814"); ac.setAppSecret("507f2dd8699bdcecd8cd8b1bb0553ff6"); ApiConfigKit.setThreadLocalApiConfig(ac); String mjson = readFile(Class.class.getClass().getResource("/menu.json").getPath()); System.out.println(mjson); ApiResult returns = MenuApi.createMenu(mjson); System.out.println(returns.getJson()); }
public static WebAccessToken getJsAccessToken(String code) { ApiConfig ac = ApiConfigKit.getApiConfig(); String json = HttpKit.get( "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + ac.getAppId() + "&secret=" + ac.getAppSecret() + "&code=" + code + "&grant_type=authorization_code"); return new WebAccessToken(json); }
private static synchronized AccessToken requestAccessToken() { AccessToken result = null; ApiConfig ac = ApiConfigKit.getApiConfig(); for (int i = 0; i < 3; i++) { String appId = ac.getAppId(); String appSecret = ac.getAppSecret(); Map<String, String> queryParas = ParaMap.create("appid", appId).put("secret", appSecret).getData(); String json = HttpKit.get(url, queryParas); result = new AccessToken(json); if (result.isAvailable()) break; } return result; }
private static synchronized WebAccessToken refreshWebAccessToken(WebAccessToken webAccessToken) { ApiConfig ac = ApiConfigKit.getApiConfig(); String json = HttpKit.get( "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + ac.getAppId() + "&grant_type=refresh_token&refresh_token=" + webAccessToken.getRefreshToken()); try { WebAccessToken newwat = new WebAccessToken(json); return newwat; } catch (Exception e) { throw new RuntimeException(e); } }
/** 强制更新 access token 值 */ public static synchronized void refreshAccessToken() { ApiConfig ac = ApiConfigKit.getApiConfig(); String corpid = ac.getCorpId(); String corpsecret = ac.getSecret(); final Map<String, String> queryParas = ParaMap.create("corpid", corpid).put("corpsecret", corpsecret).getData(); // 最多三次请求 AccessToken result = RetryUtils.retryOnException( 3, new Callable<AccessToken>() { @Override public AccessToken call() throws Exception { String json = HttpUtils.get(url, queryParas); return new AccessToken(json); } }); // 三次请求如果仍然返回了不可用的 access token 仍然 put 进去,便于上层通过 AccessToken 中的属性判断底层的情况 accessTokenCache.set(ac.getCorpId(), result); }
public static void init() { ApiConfig ac = new ApiConfig(); ac.setAppId(AppID); ac.setAppSecret(AppSecret); ApiConfigKit.setThreadLocalApiConfig(ac); }