Java:获取企业微信Token

1、获取条件:企业ID、应用的凭证密钥

官方API:https://work.weixin.qq.com/api/doc/90000/90135/91039

代码:
corpid:企业ID
corpsecret:应用的凭证密钥

  public static AccessToken getAccessToken(){
        if(StringUtils.isEmpty(corpid)){
            logger.info("corpid 不能为空");
            throw new BusinessException("corpid 不能为空");
        }
        if(StringUtils.isEmpty(corpsecret)){
            logger.info("corpsecret 不能为空");
            throw new BusinessException("corpsecret 不能为空");
        }
        AccessToken token = null;
        String url = access_token_url.replace("{corpId}",corpid).replace("{corpsecret}",corpsecret);
        logger.info("get weixin access_token url:"+url);
        JSONObject jsonObject = HttpsUtil.httpsRequest(url,"GET","");
        logger.info("get weixin access_token callback data:"+jsonObject);
        if(StringUtils.isEmpty(jsonObject)){
            logger.info("access_token返回为空,result:"+jsonObject);
            throw new BusinessException("access_token返回为空,result:"+jsonObject);
        }else{
            try {
                token = new AccessToken();
                token.setAccessToken(jsonObject.getString("access_token"));
                token.setExpiresIn(jsonObject.getInteger("expires_in"));
                token.setErrcode(jsonObject.getInteger("errcode"));
                token.setErrmsg(jsonObject.getString("errmsg"));
                /*缓存进tokenMap*/
                tokenMap.put(TOKENKEY,token);
            }catch (Exception e){
                logger.info("access_token 获取失败。errcode:"+jsonObject.getInteger("errcode")+",errmsg:"+jsonObject.getString("errmsg"));
                throw new BusinessException("access_token 获取失败。errcode:"+jsonObject.getInteger("errcode")+",errmsg:"+jsonObject.getString("errmsg"));
            }
        }
        return token;
    }

你可能感兴趣的:(Java)