public static void main(String[] args) throws IOException {
Integer agentId = *******;
String agentSecret = "*******";
String corpId = "*******";
//企微API host
String qiyeUrl = "https://qyapi.weixin.qq.com/cgi-bin";
//企微API认证地址
String authorizeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base#wechat_redirect";
String tokeUrl = qiyeUrl + "/gettoken?corpid=%s&corpsecret=%s";
//获取token
String getTokeUrl = String.format(tokeUrl, corpId, agentSecret);
String token = null;
String rtn = HttpUtil.sendHttpsRequest(getTokeUrl, "", "GET");
JsonNode jsonObj = JsonUtil.toJsonNode(rtn);
//取到了
if (jsonObj.get("errcode").asInt() == 0) {
token = jsonObj.get("access_token").asText();
}
String url, resultJson;
//-----------------发送消息-----------------
String sendMsgUrl = qiyeUrl + "/message/send?access_token=" + token;
//业务系统跳转地址(用于鉴权)
String serviceUrl = URLEncoder.encode("*******/thirdServices/userInfo/v1/wechatLogin", "utf-8");
//跳转地址 微信认证 --> 认证成功重定向 --> 获取用户code --> 获取用户id
url = String.format(authorizeUrl, corpId, serviceUrl);
WxTextCardMessage wxMsg = new WxTextCardMessage("liujintai", null, agentId + "", "新入住消息推送", DateUtil.getCurrentTime("yyyy年MM月dd日"), "", "", url, "详情");
resultJson = HttpUtil.sendHttpsRequest(sendMsgUrl, JsonUtil.toJson(wxMsg), WeChatWorkConsts.METHOD_POST);
System.out.println(resultJson);
//-----------------发送消息-----------------
//-----------------获取用户信息-----------------
//企微认证成功,从定向会添加用户code http://*******/thirdServices/userInfo/v1/wechatLogin?code=UJNwYViUBTWlLNx-OVFSyfDRrZD0am6JzPOmjxJccO4&state=
String code = "UJNwYViUBTWlLNx-OVFSyfDRrZD0am6JzPOmjxJccO4";
String userId = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%s&code=%s";
url = String.format(userId, token, code);
resultJson = HttpUtil.sendHttpsRequest(url, null, WeChatWorkConsts.METHOD_GET);
System.out.println(resultJson);
//-----------------获取用户信息-----------------
}
1、发送应用消息
2、消息中配置跳转地址(微信认证,添加鉴权)
3、微信认证成功重定向到业务系统
4、根据微信code获取微信用户id
5、业务系统完成鉴权,重定向到业务前端页面