企微自建应用发送消息对接流程

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、业务系统完成鉴权,重定向到业务前端页面

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值