开启左侧

获取企微通讯录(运用)的access_token

[复制链接]
在线会员 c5BLrEy 发表于 2022-12-30 14:44:51 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
民间文档地点:获得access_token - 交心文档 - 企业微疑开辟者中间
获得企微通信录(使用)的access_token-1.jpg

获得企微通信录(使用)的access_token-2.jpg


获得企微通信录(使用)的access_token-3.jpg


按照文档请求获得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的步调
您需要登录后才可以回帖 登录 | 立即注册 qq_login

本版积分规则

发布主题
阅读排行更多+
用专业创造成效
400-778-7781
周一至周五 9:00-18:00
意见反馈:server@mailiao.group
紧急联系:181-67184787
ftqrcode

扫一扫关注我们

Powered by 职贝云数A新零售门户 X3.5© 2004-2025 职贝云数 Inc.( 蜀ICP备2024104722号 )