java调用企业微信接口发送文件功能
结合几位大佬的代码后实现此功能:感谢大佬让我完成此功能的实现,如有侵权,立刻删除。
借鉴文章地址:
java调用企业微信接口发送消息https://blog.csdn.net/zxl782340680/article/details/79876502。介绍的十分详细,相当感动,图片加代码。(这个是发送普通文本消息的)一下子代码就通过了。
corpID之类的页面参数告诉在哪里获取,支持下作者。
微信小程序客服消息新增临时素材接口java实现https://www.cnblogs.com/wbxk/p/8581195.html
代码
一、实体类:
注意:json字符串和实体类是对应的:一定要对应,我就是没有对应导致浪费一天半时间在修改40007错误。最后发现我输出出来的json和官方文档的不一样。这个是我踩的最大的坑media_id明明获取到了,确报40007media_id不合法,让我一直以为是我id获取错了,换了接口等,怎么都不好使。最后发现官方文档请求数据是这样的
我的是:
“media_id”:“获取到的media_id”
相差这么大我竟然没发现,最后还是我公司哥给我发现的。希望自己以后认真一点吧。
/**
* 微信消息发送实体类
* @author
*/
public class WeChatData {
//发送微信消息的URLString sendMsgUrl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
/**
* 成员账号
*/
private String touser;
/**
* 消息类型
*/
private String msgtype;
/**
* 企业应用的agentID
*/
private int agentid;
/**
* 实际接收Map类型数据
*/
private Object file;
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;
}
public Object getFile() {
return file;
}
public void setFile(Object file) {
this.file = file;
}
}
二、微信授权请求
public class WeChatUrlData {
/**
* 企业Id
*/
private String corpid;
/**
* secret管理组的凭证密钥
*/
private String corpsecret;
/**
* 获取ToKen的请求
*/
private String Get_Token_Url;
/**
* 发送消息的请求
*/
private 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;
}
}
三、微信发送消息
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client