java调用企业微信接口API发送消息(替代短信验证码的一种解决方案)
文章采纳源地址:https://www.cnblogs.com/kuzaman/p/6564632.html
使用微信发送验证码配置页面:
以下步骤获取页面上的参数
1、注册企业微信:
注册网址: https://work.weixin.qq.com/
注册成功后微信扫码/企业微信扫码进行登陆
登陆成功页面:
2、获取企业ID
3、点击企业应用创建一个用来发送验证码的应用
验证码应用页面:这里获取企业应用的id【agentID】和管理组的凭证秘钥【CorpSecret】
4、body是你要发送的文本信息【可以是任意内容但是有文本长度的限制】
5、ToUser这个参数是成员的账号【大小写都可以】
6、如何加入新的成员
扫描下方二维码下载企业微信,注册加入企业
扫描下方二维码,关注企业微信插件即可通过微信接収消息
7、后台代码:
依赖包:
package com.wechat.send;
/**
* 微信消息发送实体类
*
@author
zhangmingliang
*/
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
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;
}
}
package com.ultrapower.umap.entity.factor;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
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.methods.HttpGet;
import org.apache.http.client.methods.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;
/**
* 微信发送消息
* @author zhangmingliang
*/
public class WeChatMsgSend {
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;
}
/**corpid应用组织编号 corpsecret应用秘钥
* 获取toAuth(String Get_Token_Url)返回结果中键值对中access_token键的值
* @param
*/
public String getToken(String corpid,String corpsecret) throws IOException {
WeChatMsgSend sw = new WeChatMsgSend();
WeChatUrlData uData = new WeChatUrlData();
uData.setGet_Token_Url(corpid,corpsecret);
String resp = sw.toAuth(uData.getGet_Token_Url());
System.out.println("resp=====:"+resp);
try{
Map map = gson.fromJson(resp,new TypeToken>() {}.getType());
return map.get("access_token").toString();
}catch (Exception e) {
return resp;
}
}
/**
* @Title:创建微信发送请求post数据
* touser发送消息接收者 ,msgtype消息类型(文本/图片等),
* application_id应用编号。
* 本方法适用于text型微信消息,contentKey和contentValue只能组一对
* @return String
*/
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);
wcd.setText(content);
return gson.toJson(wcd);
}
/**
* @Title 创建微信发送请求post实体
* charset消息编码 ,contentType消息体内容类型,
* url微信消息发送请求地址,data为post数据,token鉴权token
* @return String
*/
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;
}
}
package com.wechat.send;
/**
* 微信授权请求
*
@author
zhangmingliang
*/
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 ;
}
}
package com.wechat.send;
import java.io.IOException;
public
class Test {
public
static
void main(String[] args) {
WeChatMsgSend swx =
new WeChatMsgSend();
try {
String token = swx.getToken(
"wwbae2c89178c02d3f" ,
"hQR5YpgtxY8B3d9S5hmTzYEgiD2yvlibHk1H5hTp6Ms" );
String postdata = swx.createpostdata(
"ZhangMingLiang" ,
"text" , 1000002,
"content" ,
"This alert Email come from IapppayBJQA" );
String resp = swx.post(
"utf-8" , WeChatMsgSend.
CONTENT_TYPE ,(
new WeChatUrlData()).getSendMessage_Url(), postdata, token);
System.
out .println(
"获取到的token======>" + token);
System.
out .println(
"请求数据======>" + postdata);
System.
out .println(
"发送微信的响应数据======>" + resp);
}
catch (IOException e) {
e.printStackTrace();
}
}
}