企业微信API之通讯录同步

同步企业微信中的员工信息到本地数据库

package com.bh.module.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bh.common.utils.HttpUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WeChatDevelopService {

   //获取access_token url
   private static String contactsTokenURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
   //根据获取单个用户url
   private static String doGetUserList = "https://qyapi.weixin.qq.com/cgi-bin/user/get";
   //企业id(唯一)
   private static String corpid = "*********";
   // 企业通讯录密钥
   private static String corpsecret = "***********************";
   // 获取部门列表url
   private static final String getDeptListURL = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist";
   //获取token
   private static String access_token;

   static {
       try {
           access_token = WeChatDevelopService.doGetAccessToken();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

   /**
    * 获取所有通讯录成员
    * 因为我们要的是所有成员的列表
    * 遍历获取
    * @return
    * @throws Exception
    */
   public static JSONArray getUserLists() throws Exception {
       JSONArray array = new JSONArray();
       if (WeChatDevelopService.getDeptList().get("errcode").toString().equals("0")) {
           JSONObject userList = (JSONObject) WeChatDevelopService.getDeptList();
           JSONArray jsonArray = (JSONArray) userList.get("userlist");
           for (int i = 0; i < jsonArray.size(); i++) {
               JSONObject job = jsonArray.getJSONObject(i);
               WeChatDevelopService.oneEmployee(job.get("userid").toString());
               JSONObject contactList = (JSONObject) WeChatDevelopService.oneEmployee(job.get("userid").toString());
               array.add(contactList);
           }
       }
       return array;
   }


   /**
    * 获取通讯录中单个成员的信息
    *
    * @return
    * @throws Exception
    */
   public static JSONObject oneEmployee(String name) throws Exception {
       //创建一个httpclient对象
       CloseableHttpClient httpClient = HttpClients.createDefault();
       //创建一个uri对象
       URIBuilder uriBuilder = new URIBuilder(doGetUserList);
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

       nameValuePairs.add(new BasicNameValuePair("access_token", access_token));
       nameValuePairs.add(new BasicNameValuePair("userid", name));
       uriBuilder.addParameters(nameValuePairs);
       HttpGet get = new HttpGet(uriBuilder.build());
       //执行请求
       CloseableHttpResponse response = httpClient.execute(get);
       //取响应的结果
       int statusCode = response.getStatusLine().getStatusCode();

       HttpEntity entity = response.getEntity();
       String objectStr = EntityUtils.toString(entity, "utf-8");
       JSONObject jsonObject = JSONObject.parseObject(objectStr);
       //关闭httpclient
       response.close();
       httpClient.close();
       return jsonObject;
   }


   /**
    * 获取部门列表
    * 参数:access_token
    * department_id 部门id
    *
    * @return
    * @throws Exception
    */
   public static JSONObject getDeptList() throws Exception {
       //创建一个httpclient对象
       CloseableHttpClient httpClient = HttpClients.createDefault();
       //创建一个uri对象
       URIBuilder uriBuilder = new URIBuilder(getDeptListURL);
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
       nameValuePairs.add(new BasicNameValuePair("access_token", access_token));
       nameValuePairs.add(new BasicNameValuePair("department_id", "1"));
       nameValuePairs.add(new BasicNameValuePair("fetch_child", "1"));
       uriBuilder.addParameters(nameValuePairs);
       HttpGet get = new HttpGet(uriBuilder.build());
       //执行请求
       CloseableHttpResponse response = httpClient.execute(get);
       //取响应的结果
       int statusCode = response.getStatusLine().getStatusCode();
       HttpEntity entity = response.getEntity();
       String objectStr = EntityUtils.toString(entity, "utf-8");
       JSONObject jsStr = JSONObject.parseObject(objectStr);

       //关闭httpclient
       response.close();
       httpClient.close();
       return jsStr;
   }


   /**
    * 获取 access_token 信息
    *
    * @return
    * @throws Exception
    */
   public static String doGetAccessToken() throws Exception {
       //创建一个httpclient对象
       CloseableHttpClient httpClient = HttpClients.createDefault();
       //创建一个uri对象
       URIBuilder uriBuilder = new URIBuilder(contactsTokenURL);
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

       nameValuePairs.add(new BasicNameValuePair("corpid", corpid));
       nameValuePairs.add(new BasicNameValuePair("corpsecret", corpsecret));
       uriBuilder.addParameters(nameValuePairs);
       HttpGet get = new HttpGet(uriBuilder.build());
       //执行请求
       CloseableHttpResponse response = httpClient.execute(get);
       //取响应的结果
       int statusCode = response.getStatusLine().getStatusCode();
       HttpEntity entity = response.getEntity();
       String jsonStr = EntityUtils.toString(entity, "utf-8");

       JSONObject jsStr = JSONObject.parseObject(jsonStr);
       String access_token = jsStr.getString("access_token");

       //关闭httpclient
       response.close();
       httpClient.close();
       return access_token;
   }

}

欢迎补充~~~~

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值