微信企业号登录授权Java实现获取员工userid根据userid换openid

来源:互联网 发布:client是什么软件 编辑:程序博客网 时间:2024/04/30 11:12

微信企业号登录授权Java实现获取员工userid根据userid换openid 2016年1月8日

为了方便测试 。debug 建议大家搞个花生壳或者其他的可以映射公网IP的软件。

貌似涨价了8元一个。鄙人捡了个便宜1元买的。还是个二级域名

1.设置一个菜单调用授权接口的URL (https://open.weixin.qq.com/和这个网站的是不一样的东西)获取code

   https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECTURI&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect


2.写一个方法接收上图中reurl 获取code access_token

public String execute() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String code = request.getParameter("code");
        if(!"authdeny".equals(code)){
            String access_token =  WeixinUtil.getAccessToken(您的企业号corpId,您的企业号secret).getToken();
            String UserID = oAuth2Service.getUserID(access_token, code, "2"); //第3步
            request.setAttribute("UserId", UserID);
        }
        request.getRequestDispatcher("/index.jsp").forward(request,response);
        return null;
    }
 


3.getUserID获取员工userid 这个id并不是用户openid 


/**
* 获取员工信息的接口地址
**/
public  String CODE_TO_USERINFO = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE&agentid=AGENTID";
 
 
/**
     * 根据code获取成员信息
     * @param access_token 调用接口凭证
     * @param code   通过员工授权获取到的code,每次员工授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期
     * @param agentid   跳转链接时所在的企业应用ID 管理员须拥有agent的使用权限;agentid必须和跳转链接时所在的企业应用ID相同
     * */
    public  String getUserID(String access_token, String code, String agentid) {
        String UserId = "";
        CODE_TO_USERINFO = CODE_TO_USERINFO.replace("ACCESS_TOKEN", access_token).replace("CODE", code).replace("AGENTID", agentid);
        JSONObject jsonobject = WeixinUtil.httpRequest(CODE_TO_USERINFO, "GET"null);
        if (null != jsonobject) {
            UserId = jsonobject.getString("UserId");
            if (!"".equals(UserId)) {
                System.out.println("获取信息成功,o(∩_∩)o ————UserID:" + UserId);
            else {
                int errorrcode = jsonobject.getInt("errcode");
                String errmsg = jsonobject.getString("errmsg");
                String error = "错误码:" + errorrcode + "————" "错误信息:" + errmsg;
                log.error(error);
            }
        else {
            log.error("获取授权失败了");
        }
        return UserId;
    }
  


4.再根据第2步的转发 将获取的数据传递到页面 测试是否正确

4.1 首先点击微信企业号里面有授权菜单的应用 的菜单

4.2 走第2步的Action 获取code、 access_token换取userid 并转发到指定页面


整个过程就是这样的简单

5.根据userid换取openid 需要第三步里面的方法。自己拼接json格式。或者直接返回第三步里面的JSONObject 测试代码的json格式是只有userid的。


/***
     * 根据userid 换取openid
     * @param access_token 根据企业号的id 和密钥得到
     * @param param 需要的json数据。{\"userid\": \"zhangsan\"}
     * @return
     */
      
     测试的数据 String param = "{\"userid\": \"zongxiaoshuai\"}";
    public  String getOpenId(String access_token,String param) {
        String OpenId = "";
        USERID_TO_OPENID = USERID_TO_OPENID.replace("ACCESS_TOKEN", access_token);
        JSONObject jsonobject = WeixinUtil.httpRequest(USERID_TO_OPENID, "GET", param);
        System.out.println(jsonobject);
        if (null != jsonobject) {
            OpenId = jsonobject.getString("openid").toString();
            if (!"".equals(OpenId)) {
                System.out.println("获取信息成功,o(∩_∩)o ————OpenId:" + OpenId);
            else {
                int errorrcode = jsonobject.getInt("errcode");
                String errmsg = jsonobject.getString("errmsg");
                System.out.println("错误码:" + errorrcode + "————" "错误信息:" + errmsg);
            }
        else {
            System.out.println("获取OpenId失败了,●﹏●,自己找原因。。。");
        }
        return OpenId;
    }
 

正常返回


1
{"errcode":0,"errmsg":"ok","openid":"ov88GjwZWGL7PiFc6MLhagoNzQ94"}

0 0
原创粉丝点击