做農(nóng)產(chǎn)品網(wǎng)站需要做的準(zhǔn)備谷歌app官方下載
這里主要針對(duì)的是企業(yè)微信服務(wù)商代開(kāi)發(fā)模式 文檔地址
可以看到里面大致有三種token,一個(gè)是服務(wù)商的token,一個(gè)是企業(yè)授權(quán)token,還有一個(gè)是應(yīng)用的token
這里面主要有下面幾個(gè)參數(shù)
首先是服務(wù)商的 corpid 和 provider_secret ,這個(gè)可以在 應(yīng)用管理-通用開(kāi)發(fā)參數(shù) 里面查看
然后是企業(yè)的 corpid 和企業(yè)的永久授權(quán)碼 permanent_code ,這兩個(gè)是需要在企業(yè)授權(quán)的的時(shí)候通過(guò)回調(diào)獲取的,具體請(qǐng)參考官方文檔 獲取永久授權(quán)碼
最后就是應(yīng)用的 suite_id 和 suite_secret 還需要一個(gè) suite_ticket ,前面兩個(gè)在應(yīng)用信息里面就可以看到,suite_ticket 這個(gè)也是需要通過(guò)回調(diào)獲取 ,具體參考官方文檔 推送suite_ticket
拿到這些參數(shù)后就好說(shuō)了,剩下的都是調(diào)接口
直接上代碼
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qyzj.common.base.exception.BusinessException;
import com.qyzj.common.base.util.HttpsRequestUtil;
import com.qyzj.common.redis.constant.CacheKey;
import com.qyzj.common.redis.util.RedisUtil;
import com.qyzj.service.task.constant.WeiXinProviderConst;
import com.qyzj.service.task.constant.WeixinCorpKeyConst;
import com.qyzj.service.task.constant.WeixinCorpUrlConst;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;/*** @author Sakura* @date 2024/7/2 11:48*/
@Component
@Log
public class WxTokenUtil {@Autowiredprivate RedisUtil redisUtil;// 獲取服務(wù)商token// 這里有一個(gè)問(wèn)題,企業(yè)微信本身可能會(huì)使token提前失效,所以有時(shí)需要強(qiáng)制重新獲取token,詳情見(jiàn)官方文檔// https://developer.work.weixin.qq.com/document/path/91200// 所以此處加了一個(gè)cache用來(lái)判斷是否需要走緩存拿token,正常情況默認(rèn)走緩存即可public String getProviderAccessToken(Boolean cache) {try {// 如果Redis里面有則直接取Redis里面的if (cache &&redisUtil.hasKey(CacheKey.KEY_CORP_PROVIDER_ACCESS_TOKEN)) {return redisUtil.get(CacheKey.KEY_CORP_PROVIDER_ACCESS_TOKEN).toString();}// 封裝請(qǐng)求參數(shù)JSONObject json = new JSONObject();json.put("corpid", WeiXinProviderConst.corpId);json.put("provider_secret", WeiXinProviderConst.providerSecret);// 請(qǐng)求微信接口String accessTokenStr = HttpsRequestUtil.sendPostRequest(WeixinCorpUrlConst.PROVIDER_ACCESS_TOKEN, json.toJSONString());log.info("get provider access token return :" + accessTokenStr);JSONObject accessTokenJson = JSON.parseObject(accessTokenStr);// 獲取 access_tokenString accessToken = accessTokenJson.getString(WeixinCorpKeyConst.providerAccessToken);//官方 expires_in 為 2個(gè)小時(shí), 這里設(shè)置100分鐘redisUtil.set(CacheKey.KEY_CORP_PROVIDER_ACCESS_TOKEN, accessToken, CacheKey.expireTime100);return accessToken;} catch (Exception e) {log.info("get provider access token error");e.printStackTrace();throw new BusinessException("get provider access token error");}}// 獲取企業(yè)授權(quán)tokenpublic String getCorpAccessToken(String corpId, String permanentCode) {try {// 如果Redis里面有則直接取Redis里面的if (redisUtil.hasKey(CacheKey.KEY_CORP_ACCESS_TOKEN + "_" + corpId)) {return redisUtil.get(CacheKey.KEY_CORP_ACCESS_TOKEN + "_" + corpId).toString();}String accessTokenUrl = WeixinCorpUrlConst.CORP_ACCESS_TOKEN.replace("#{CORP_ID}", corpId).replace("#{CORP_SECRET}", permanentCode);String accessTokenStr = HttpsRequestUtil.sendGetRequest(accessTokenUrl);log.info("get corp access token return :" + accessTokenStr);JSONObject accessTokenJson = JSON.parseObject(accessTokenStr);// 獲取 access_tokenString accessToken = accessTokenJson.getString(WeixinCorpKeyConst.accessToken);//官方 expires_in 為 2個(gè)小時(shí), 這里設(shè)置 100 分鐘redisUtil.set(CacheKey.KEY_CORP_ACCESS_TOKEN + "_" + corpId, accessToken, CacheKey.expireTime100);return accessToken;} catch (Exception e) {log.info("get corp access token error");e.printStackTrace();throw new BusinessException("get corp access token error");}}// SuiteTicket是通過(guò)回調(diào)獲取的,企微每10分鐘會(huì)推送一次// 如果沒(méi)有可手動(dòng)在服務(wù)商企業(yè)微信后臺(tái)刷新public String getSuiteTicket() {// 如果Redis里面有則直接取Redis里面的if (redisUtil.hasKey(CacheKey.KEY_CORP_SUITE_TICKET)) {return redisUtil.get(CacheKey.KEY_CORP_SUITE_TICKET).toString();} else {log.info("get suite ticket error");throw new BusinessException("get suite ticket error");}}// 獲取應(yīng)用token// 注意suiteTicket是從回調(diào)接口獲取的// 注意該token需要配置ip白名單public String getSuiteAccessToken() {try {// 如果Redis里面有則直接取Redis里面的if (redisUtil.hasKey(CacheKey.KEY_SUITE_ACCESS_TOKEN)) {return redisUtil.get(CacheKey.KEY_SUITE_ACCESS_TOKEN).toString();}// 封裝請(qǐng)求參數(shù)JSONObject json = new JSONObject();json.put(WeixinCorpKeyConst.suiteId, WeiXinProviderConst.suiteId);json.put(WeixinCorpKeyConst.suiteSecret, WeiXinProviderConst.suiteSecret);json.put(WeixinCorpKeyConst.suiteTicket, getSuiteTicket());String accessTokenStr = HttpsRequestUtil.sendPostRequest(WeixinCorpUrlConst.SUITE_ACCESS_TOKEN, json.toJSONString());log.info("get suite access token return :" + accessTokenStr);JSONObject accessTokenJson = JSON.parseObject(accessTokenStr);//獲取 accessTokenString accessToken = accessTokenJson.getString(WeixinCorpKeyConst.suiteAccessToken);//官方 expires_in 為 2個(gè)小時(shí), 這里設(shè)置 100分鐘redisUtil.set(CacheKey.KEY_SUITE_ACCESS_TOKEN, accessToken, CacheKey.expireTime100);return accessToken;} catch (Exception e) {log.info("get suite access token error");e.printStackTrace();throw new BusinessException("get suite access token error");}}
}
這里面用到的 HttpsRequestUtil 工具類(lèi)
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;/*** @author Sakura* @date 2024/6/24 17:07*/
public class HttpsRequestUtil {public static String sendPostRequest(String urlString, String jsonInputString) throws IOException {URL url = new URL(urlString);HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 設(shè)置請(qǐng)求方法為POSTconnection.setRequestMethod("POST");// 設(shè)置請(qǐng)求頭connection.setRequestProperty("Content-Type", "application/json");connection.setRequestProperty("Accept", "application/json");// 啟用輸出流,用于發(fā)送請(qǐng)求數(shù)據(jù)connection.setDoOutput(true);try (OutputStream os = connection.getOutputStream()) {byte[] input = jsonInputString.getBytes("utf-8");os.write(input, 0, input.length);}// 獲取響應(yīng)try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {StringBuilder response = new StringBuilder();String responseLine;while ((responseLine = br.readLine()) != null) {response.append(responseLine.trim());}return response.toString();} finally {// 關(guān)閉連接connection.disconnect();}}public static String sendGetRequest(String urlString) throws IOException {URL url = new URL(urlString);HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 設(shè)置請(qǐng)求方法為GETconnection.setRequestMethod("GET");// 設(shè)置請(qǐng)求頭connection.setRequestProperty("Accept", "application/json");// 獲取響應(yīng)try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {StringBuilder response = new StringBuilder();String responseLine;while ((responseLine = br.readLine()) != null) {response.append(responseLine.trim());}return response.toString();} finally {// 關(guān)閉連接connection.disconnect();}}}
還有企業(yè)微信的兩個(gè)基本配置類(lèi)
public class WeixinCorpUrlConst {/*** 獲取 provider_access_token 的 url*/public static final String PROVIDER_ACCESS_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/service/get_provider_token";/*** 獲取 suite_access_token 的 url*/public static final String SUITE_ACCESS_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token";/*** 獲取 pre_auth_code 的 url*/public static final String PRE_AUTH_CODE = "https://qyapi.weixin.qq.com/cgi-bin/service/get_pre_auth_code?suite_access_token=";/*** 前端授權(quán)頁(yè)面 的 url*/public static final String URL_AUTH_PAGE = "https://open.work.weixin.qq.com/3rdapp/install";/*** 前端登錄頁(yè)面 的 url*/public static final String URL_LOGIN_PAGE = "https://open.work.weixin.qq.com/wwopen/sso/3rd_qrConnect";/*** 獲取 permanent_code 的 url*/public static final String PERMANENT_CODE = "https://qyapi.weixin.qq.com/cgi-bin/service/get_permanent_code?suite_access_token=";/*** 獲取 企業(yè)授權(quán) access_token 的 url*/public static final String CORP_ACCESS_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=#{CORP_ID}&corpsecret=#{CORP_SECRET}";/*** 獲取 企業(yè)應(yīng)用 access_token 的 url*/public static final String ACCESS_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";/*** 獲取 應(yīng)用信息 的 url*/public static final String AGENT_INFO = "https://qyapi.weixin.qq.com/cgi-bin/agent/get?access_token=#{ACCESS_TOKEN}&agentid=#{AGENT_ID}";/*** 獲取 部門(mén)列表 的 url*/public static final String DEPARTMENT_LIST = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=#{ACCESS_TOKEN}";/*** 獲取 部門(mén)成員列表 的 url*/public static final String USER_LIST = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=#{ACCESS_TOKEN}&department_id=#{DEPARTMENT_ID}&fetch_child=1";/*** 獲取 成員信息 的 url*/public static final String USER_INFO = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=#{ACCESS_TOKEN}&userid=#{USER_ID}";/*** 獲取 企業(yè)管理員 的 url*/public static final String ADMIN_USER_LIST = "https://qyapi.weixin.qq.com/cgi-bin/service/get_admin_list?suite_access_token=";/*** 第三方應(yīng)用:獲取 企業(yè)成員登錄信息 的 url*/public static final String PROVIDER_LOGIN_INFO = "https://qyapi.weixin.qq.com/cgi-bin/service/get_login_info?access_token=";/*** 自建應(yīng)用:獲取 企業(yè)成員登錄信息 的 url*/public static final String LOCAL_LOGIN_INFO = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=#{ACCESS_TOKEN}&code=#{CODE}";public static final String LOGIN_DETAIL_INFO = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token=#{ACCESS_TOKEN}";/*** 批量獲取 客戶(hù)列表 的 url*/public static final String BATCH_CUSTOMER_LIST = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token=#{ACCESS_TOKEN}";/*** 獲取 客戶(hù)詳情 的 url*/public static final String CUSTOMER_INFO = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token=#{ACCESS_TOKEN}&external_userid=#{EXTERNAL_USERID}";/*** 修改 客戶(hù)備注信息 的url*/public static final String UPDATE_CUSTOMER_REMARK = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/remark?access_token=#{ACCESS_TOKEN}";/*** 編輯 客戶(hù)標(biāo)簽 的url*/public static final String MARK_CUSTOMER_TAG = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/mark_tag?access_token=#{ACCESS_TOKEN}";/*** 獲取 企業(yè)標(biāo)簽 的 url*/public static final String TAG_GROUP_LIST = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list?access_token=";/*** 添加 企業(yè)標(biāo)簽 的 url*/public static final String ADD_TAG_GROUP = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag?access_token=";/*** 修改 企業(yè)標(biāo)簽 的 url*/public static final String UPDATE_TAG_GROUP = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag?access_token=";/*** 刪除 企業(yè)標(biāo)簽 的 url*/public static final String DELETE_TAG_GROUP = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag?access_token=";/*** 獲取 客戶(hù)群列表 的 url*/public static final String GROUP_CHAT_LIST = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/list?access_token=#{ACCESS_TOKEN}";/*** 獲取 客戶(hù)群詳情 的 url*/public static final String GROUP_CHAT_INFO = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/get?access_token=#{ACCESS_TOKEN}";/*** 查詢(xún) 聯(lián)系我 的 url*/public static final String GET_CONTACT_WAY = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_contact_way?access_token=#{ACCESS_TOKEN}";/*** 添加 聯(lián)系我 的 url*/public static final String ADD_CONTACT_WAY = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_contact_way?access_token=#{ACCESS_TOKEN}";/*** 更新 聯(lián)系我 的 url*/public static final String UPDATE_CONTACT_WAY = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/update_contact_way?access_token=#{ACCESS_TOKEN}";/*** 刪除 聯(lián)系我 的 url*/public static final String DEL_CONTACT_WAY = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_contact_way?access_token=#{ACCESS_TOKEN}";/*** 發(fā)送 歡迎語(yǔ) 的 url*/public static final String SEND_WELCOME_MSG = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/send_welcome_msg?access_token=#{ACCESS_TOKEN}";/*** 上傳 臨時(shí)素材 的 url*/public static final String UPLOAD_MEDIA = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=";/*** 添加 入群歡迎語(yǔ)素材 的 url*/public static final String ADD_GROUP_WELCOME_TEMPLATE = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/add?access_token=#{ACCESS_TOKEN}";/*** 更新 入群歡迎語(yǔ)素材 的 url*/public static final String UPDATE_GROUP_WELCOME_TEMPLATE = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/edit?access_token=#{ACCESS_TOKEN}";/*** 刪除 入群歡迎語(yǔ)素材 的 url*/public static final String DEL_GROUP_WELCOME_TEMPLATE = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/del?access_token=#{ACCESS_TOKEN}";/*** 發(fā)送 應(yīng)用消息 的 url*/public static final String SEND_APPLICATION_MESSAGE = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=#{ACCESS_TOKEN}";/*** 獲取待分配的離職成員列表 的url*/public static final String DIMISSION_WAIT_ALLOT_CLIENT = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_unassigned_list?access_token=#{ACCESS_TOKEN}";/*** 分配離職成員的客戶(hù)*/public static final String DIMISSION_ALLOT_Client = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/resigned/transfer_customer?access_token=#{ACCESS_TOKEN}";/*** 分配離職成員的客戶(hù)群*/public static final String DIMISSION_ALLOT_GROUP = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/transfer?access_token=#{ACCESS_TOKEN}";/*** 創(chuàng)建企業(yè)群發(fā)*/public static final String ADD_CORP_MASS_URL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_msg_template?access_token=#{ACCESS_TOKEN}";/*** 編輯客戶(hù)企業(yè)標(biāo)簽*/public static final String EDIT_CUSTOMER_CORP_TAG = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/mark_tag?access_token=#{ACCESS_TOKEN}";/*** 獲取企業(yè)的jsapi_ticket*/public static final String CORP_JS_API_TICKET = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=";/*** 獲取應(yīng)用的jsapi_ticket*/public static final String AGENT_JS_API_TICKET = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?type=agent_config&access_token=";/*** 獲取 開(kāi)通了會(huì)話(huà)存檔的員工*/public static final String MSG_AUDIT_EMPLOYEE = "https://qyapi.weixin.qq.com/cgi-bin/msgaudit/get_permit_user_list?access_token=#{ACCESS_TOKEN}";/*** corpid轉(zhuǎn)換 的 url*/public static final String CORPID_TO_OPENCORPID = "https://qyapi.weixin.qq.com/cgi-bin/service/corpid_to_opencorpid?provider_access_token=";/*** userid轉(zhuǎn)換 的 url*/public static final String USERID_TO_OPENUSERID = "https://qyapi.weixin.qq.com/cgi-bin/batch/userid_to_openuserid?access_token=";public static final String GET_NEW_EXTERNAL_USERID = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_new_external_userid?access_token=";public static final String UNIONID_TO_EXTERNAL_USERID = "https://qyapi.weixin.qq.com/cgi-bin/idconvert/unionid_to_external_userid?access_token=";public static final String EXTERNAL_USERID_TO_PENDING_ID = "https://qyapi.weixin.qq.com/cgi-bin/idconvert/batch/external_userid_to_pending_id?access_token=";public static final String FINISH_OPENID_MIGRATION = "https://qyapi.weixin.qq.com/cgi-bin/service/finish_openid_migration?provider_access_token=";
}
public class WeixinCorpKeyConst {public static String errcode = "errcode";public static String errmsg = "errmsg";// 服務(wù)商相關(guān)public static String corpId = "corpid";public static String authCorpId = "auth_corpid";public static String corpName = "corp_name";public static String suiteId = "suite_id";public static String suiteSecret = "suite_secret";public static String providerSecret = "provider_secret";public static String corpSecret = "corpsecret";public static String suiteTicket = "suite_ticket";public static String suiteAccessToken = "suite_access_token";public static String providerAccessToken = "provider_access_token";public static String preAuthCode = "pre_auth_code";public static String authCode = "auth_code";public static String permanentCode = "permanent_code";public static String authCorpInfo = "auth_corp_info";public static String authInfo = "auth_info";public static String agent = "agent";public static String agentId = "agentid";public static String accessToken = "access_token";}
WeiXinProviderConst 就不貼了,里面就是記錄上面那幾個(gè)參數(shù)的
到這里可以發(fā)現(xiàn),其實(shí)對(duì)接企業(yè)微信并不難,麻煩的地方就在于各種配置各種參數(shù),然后就是回調(diào),回調(diào)這個(gè)后面有空我也會(huì)整理出來(lái)