java调用企业微信接口发送消息 您所在的位置:网站首页 qq发消息怎样分段 java调用企业微信接口发送消息

java调用企业微信接口发送消息

2024-01-25 16:07| 来源: 网络整理| 查看: 265

企业微信官网服务API地址:https://work.weixin.qq.com/api/doc/90001/90143/91201

1、注册企业微信 获取企业ID:在注册后:我的企业最下面 在这里插入图片描述 在这里插入图片描述 2、在应用管理 在这里插入图片描述 中创建应用 在这里插入图片描述 然后进入应用获取:应用id(agentID)和管理组的凭证秘钥【CorpSecret】=Secret 在这里插入图片描述 依赖:

com.google.code.gson gson 2.8.2 org.apache.httpcomponents httpclient 4.5.2 org.apache.httpcomponents httpcore 4.4.5

用到的UrlData类和WeChatData

import org.springframework.stereotype.Component; @Component public class UrlData { //应用组织编号(企业ID) corpsecret应用秘钥 String corpid; // corpsecret应用秘钥 管理组的凭证秘钥【CorpSecret】 用的 企业号/团队号 Secret String corpsecret; //获取ToKen的请求 String Get_Token_Url; //发送消息的请求 String SendMessage_Url; public String getCorpid() { return corpid; } public void setCorpid(String corpid) { this.corpid = corpid; } public String getCorpsecret() { return corpsecret; } public void setCorpsecret(String corpsecret) { this.corpsecret = corpsecret; } public void setGet_Token_Url(String corpid,String corpsecret) { this.Get_Token_Url ="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret; } public String getGet_Token_Url() { return Get_Token_Url; } public String getSendMessage_Url(){ SendMessage_Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="; return SendMessage_Url; } @Override public String toString() { return "UrlData{" + "corpid='" + corpid + '\'' + ", corpsecret='" + corpsecret + '\'' + ", Get_Token_Url='" + Get_Token_Url + '\'' + ", SendMessage_Url='" + SendMessage_Url + '\'' + '}'; } }

WeChatData 类

import org.springframework.stereotype.Component; @Component public class WeChatData { //“微信消息发送”的post数据对象文件 //发送微信消息的URL // String sendMsgUrl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="; //成员账号 String touser; //消息类型 String msgtype; //企业应用的agentID int agentid; //实际接收Map类型数据 Object text; public Object getText() { return text; } public void setText(Object text) { this.text = text; } public String getMsgtype() { return msgtype; } public void setMsgtype(String msgtype) { this.msgtype = msgtype; } public int getAgentid() { return agentid; } public void setAgentid(int agentid) { this.agentid = agentid; } public String getTouser() { return touser; } public void setTouser(String touser) { this.touser = touser; } }

get请求信息(请求地址可以参考官网)

import java.io.IOException; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Map; import java.util.Set; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.bigdata.bean.UrlData; import com.bigdata.bean.WeChatData; import org.apache.http.HttpEntity; import org.apache.http.clienthods.CloseableHttpResponse; import org.apache.http.clienthods.HttpGet; import org.apache.http.clienthods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.slf4j.LoggerFactory; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.springframework.stereotype.Component; @Component public class Send_weChatMsg { //“发送微信实体”代码 private CloseableHttpClient httpClient; private HttpPost httpPost;//用于提交登陆数据 private HttpGet httpGet;//用于获得登录后的页面 public static final String CONTENT_TYPE = "Content-Type"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// private static Gson gson = new Gson(); /** * 微信授权请求,GET类型,获取授权响应,用于其他方法截取token * @param Get_Token_Url * @return String 授权响应内容 * @throws IOException */ protected String toAuth(String Get_Token_Url) throws IOException { httpClient = HttpClients.createDefault(); httpGet = new HttpGet(Get_Token_Url); CloseableHttpResponse response = httpClient.execute(httpGet); String resp; try { HttpEntity entity = response.getEntity(); resp = EntityUtils.toString(entity, "utf-8"); EntityUtils.consume(entity); } finally { response.close(); } LoggerFactory.getLogger(getClass()).info(" resp:{}", resp); return resp; } /** * 获取toAuth(String Get_Token_Url)返回结果中键值对中access_token键的值 * @param */ public String getToken(String corpid,String corpsecret) throws IOException { Send_weChatMsg sw = new Send_weChatMsg(); UrlData uData = new UrlData(); uData.setGet_Token_Url(corpid,corpsecret); String resp = sw.toAuth(uData.getGet_Token_Url()); Map map = gson.fromJson(resp, new TypeToken() { }.getType()); return map.get("access_token").toString(); } public String createpostdata(String touser, String msgtype, int application_id, String contentKey ,String contentValue) { WeChatData wcd = new WeChatData(); wcd.setTouser(touser); wcd.setAgentid(application_id); wcd.setMsgtype(msgtype); Map content = new HashMap(); content.put(contentKey,contentValue+"\n"); wcd.setText(content); return gson.toJson(wcd); } public String post(String charset, String contentType, String url, String data,String token) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); httpPost = new HttpPost(url+token); httpPost.setHeader(CONTENT_TYPE, contentType); httpPost.setEntity(new StringEntity(data, charset)); CloseableHttpResponse response = httpclient.execute(httpPost); String resp; try { HttpEntity entity = response.getEntity(); resp = EntityUtils.toString(entity, charset); EntityUtils.consume(entity); } finally { response.close(); } LoggerFactory.getLogger(getClass()).info( "call [{}], param:{}, resp:{}", url, data, resp); return resp; } //获取部门员工 public Set getDepartmentData(String access_token,int department_id,int fetch_child) throws IOException { String Url="https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token="+access_token +"&department_id="+department_id+"&fetch_child="+fetch_child; httpClient = HttpClients.createDefault(); httpGet = new HttpGet(Url); CloseableHttpResponse response = httpClient.execute(httpGet); String resp; try { HttpEntity entity = response.getEntity(); resp = EntityUtils.toString(entity, "utf-8"); EntityUtils.consume(entity); } finally { response.close(); } LoggerFactory.getLogger(getClass()).info(" resp:{}", resp); JSONObject jsonObject = JSONObject.parseObject(resp); JSONArray userlist = jsonObject.getJSONArray("userlist"); HashMap map = new HashMap(); //遍历jsonarray 数组 if(userlist.size()>0){ for(int i=0;i0){ for(int i=0;i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有