java調用api接口,java訪問微信接口發送消息

 2023-11-18 阅读 25 评论 0

摘要:最近在開發activiti流程的時候有個需求:流程到達每個審批節點后,需要向該節點的審批人發送一個消息,提示有審批需要處理。 參考了一下微信的開發者文檔和網絡上的一些技術博客,現在記錄一下。以便后續繼續研究微信開發。【微信開發真的很好玩的】

最近在開發activiti流程的時候有個需求:流程到達每個審批節點后,需要向該節點的審批人發送一個消息,提示有審批需要處理。

參考了一下微信的開發者文檔和網絡上的一些技術博客,現在記錄一下。以便后續繼續研究微信開發。【微信開發真的很好玩的】

首先,你需要注冊一個企業微信賬號:https://work.weixin.qq.com/wework_admin/register_wx?from=myhome

?然后進入后臺管理,創建一個應用

java調用api接口、

上面標紅的信息,都是開發測試需要用到的,需要記錄一下。

先貼代碼:

WeChatData.java

package com.xfma.wx.test;/*** 微信消息發送實體類* @author PC-MXF**/
public class WeChatData {//發送微信消息的URLString sendMsgUrl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";/*** 成員賬號*/private String touser;/*** 消息類型*/private String msgtype;/*** 企業用用的agentid*/private String agentid;/*** 十幾接收map類型數據*/private Object text;public String getTouser() {return touser;}public void setTouser(String touser) {this.touser = touser;}public String getMsgtype() {return msgtype;}public void setMsgtype(String msgtype) {this.msgtype = msgtype;}public String getAgentid() {return agentid;}public void setAgentid(String agentid) {this.agentid = agentid;}public Object getText() {return text;}public void setText(Object text) {this.text = text;}}

  WeChatUrlData.java

package com.xfma.wx.test;/*** 微信授權請求* @author PC-MXF**/
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 String getGet_Token_Url() {return Get_Token_Url;}public void setGet_Token_Url(String corpid,String corpsecret) {Get_Token_Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret;}public String getSendMessage_Url() {SendMessage_Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";return SendMessage_Url;}public void setSendMessage_Url(String sendMessage_Url) {SendMessage_Url = sendMessage_Url;}}

java接口接收json數據、  WeChatMsgSend.java

package com.xfma.wx.test;import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;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;import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;/*** 微信發送消息* * @author PC-MXF**/
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);} catch (Exception e) {e.getStackTrace();} 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<String, Object> map = gson.fromJson(resp, new TypeToken<Map<String, Object>>() {}.getType());return map.get("access_token").toString();} catch (Exception e) {e.getStackTrace();return resp;}}/*** 創建微信發送請求post數據 touser發送消息接收者 ,msgtype消息類型(文本/圖片等), application_id應用編號。* 本方法適用于text型微信消息,contentKey和contentValue只能組一對* * @param touser* @param msgtype* @param application_id* @param contentKey* @param contentValue* @return*/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<Object, Object> content = new HashMap<Object, Object>();content.put(contentKey, contentValue);wcd.setText(content);return gson.toJson(wcd);}/*** @Title  創建微信發送請求post實體,charset消息編碼    ,contentType消息體內容類型,* url微信消息發送請求地址,data為post數據,token鑒權token* @param charset* @param contentType* @param url* @param data* @param token* @return* @throws IOException*/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;}
}

  Test.java

package com.xfma.wx.test;public class Test {public static void main(String[] args) {WeChatMsgSend swx = new WeChatMsgSend();try {String token = swx.getToken("ww44a4ede4d3626a","w6tRMGfN5WSFarMmqdpkwBztPRy-HVXRRs76QXvFU");String postdata = swx.createpostdata("jiangpin", "text", 1000009, "content","這是一條測試信息");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 (Exception e) {e.getStackTrace();}}
}

  

轉載于:https://www.cnblogs.com/blog411032/p/9716813.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/173992.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息