职贝云数AI新零售门户

标题: 获取企微通讯录(运用)的access_token [打印本页]

作者: c5BLrEy    时间: 2022-12-30 14:44
标题: 获取企微通讯录(运用)的access_token
官方文档地址:获取access_token - 接口文档 - 企业微信开发者中心
(, 下载次数: 2)

(, 下载次数: 3)


(, 下载次数: 3)


根据文档要求获取corpid、agentid和corpsecret
建立wechat.properties文件
  1. #企业id
  2. corpid = corpid
  3. #企业通讯录密钥
  4. corpsecret = corpsecret
  5. #获取access_token
  6. access_token_url = https://qyapi.weixin.qq.com/cgi-bin/gettoken
  7. #企业微信access_token缓存redis
  8. access_token_reids_key = wechat_access_token
  9. #运用ID
  10. a_agentid = a_agentid
  11. #运用密钥
  12. a_corpsecret = a_corpsecret
  13. #运用access_token缓存redis
  14. a_access_token_reids_key = a_access_token_reids_key
复制代码
建立WechatUtil工具类
  1. public class WechatUtil {
  2.     /**
  3.          * redis缓存有效工夫,秒
  4.          */
  5.         public static final int access_token_time = 7100;
  6.         /**
  7.          * 企业id
  8.          */
  9.         public static final String corpid;
  10.         /**
  11.          * 企业通讯录密钥
  12.          */
  13.         public static final String corpsecret;
  14.         /**
  15.          * 获取access_token
  16.          */
  17.         public static final String access_token_url;
  18.         /**
  19.          * 企业微信access_token缓存redis
  20.          */
  21.         public static final String access_token_reids_key;
  22.         // 运用
  23.         public static final String a_agentid;
  24.         public static final String a_corpsecret;
  25.         public static final String a_access_token_reids_key;
  26.     static {
  27.                 try {
  28.                         InputStream is = WechatUtil.class.getResourceAsStream("/wechat.properties");
  29.                         Properties pro = new Properties();
  30.                         pro.load(is);
  31.                         corpid = pro.getProperty("corpid");
  32.                         corpsecret = pro.getProperty("corpsecret");
  33.                         access_token_url = pro.getProperty("access_token_url");
  34.                         access_token_reids_key = pro.getProperty("access_token_reids_key");
  35.                         a_agentid= pro.getProperty("a_agentid");
  36.                         a_corpsecret= pro.getProperty("a_corpsecret");
  37.                         a_access_token_reids_key= pro.getProperty("a_access_token_reids_key");
  38.                 } catch (IOException e) {
  39.                         throw new RuntimeException("加载wechat.properties配置文件失败." + e.getMessage(), e);
  40.                 }
  41.         }
  42.     /**
  43.          * Description: 获取token(新增运用,应修订此方法)
  44.          * @param appName 传null或者空,默许查询通讯录token
  45.          * @return
  46.          * @throws Exception
  47.          */
  48.         public static String getToken(String appName) throws Exception {
  49.                 try {
  50.                         String tokenReidsKey = access_token_reids_key;
  51.                         String key = corpid;
  52.                         String secret = corpsecret;
  53.                         if (StringUtils.isEmpty(appName)) {
  54.                                 // 用默许
  55.                         } else if (appName.equals("a")) {
  56.                                 // 运用
  57.                                 tokenReidsKey = a_access_token_reids_key;
  58.                                 secret = a_corpsecret;
  59.                         }
  60.                         String token = RedisUtil.getValue(tokenReidsKey);
  61.                         if (!StringUtils.isEmpty(token)) {
  62.                                 return token;
  63.                         }
  64.                         // 获取token
  65.                         String result = HttpsUtil.getHttp(access_token_url, "corpid=" + key + "&corpsecret=" + secret);
  66.                         Map<String, Object> resultMap = orderSimpleResult(result);
  67.                         token = resultMap.get("access_token") + "";
  68.                         if (StringUtils.isEmpty(token)) {
  69.                                 throw new Exception (-1, result);
  70.                         }
  71.                         RedisUtil.setValue(tokenReidsKey, token, access_token_time);
  72.                         return token;
  73.                 } catch (Exception e) {
  74.                         throw new Exception (e);
  75.                 }
  76.         }
  77. }
复制代码
自此即完成获取企微access_token的步骤




欢迎光临 职贝云数AI新零售门户 (https://www.taojin168.com/cloud/) Powered by Discuz! X3.5