微信公众号——后台连接

2017-10-07  本文已影响0人  我_在路上

微信公众号只有在配置后台的时候才能在自己的后台接收微信公众号的消息,因此要想在微信公众号中使用自定义后台,首先必须先进行微信公众号与自己后台的对接。

将自己的后台与微信公众号连接所需的配置:
  1. 在微信公众号的基本配置中配置服务器地址、令牌、消息加解密密钥,如图所示:



注:
 服务器地址:为具体的接口地址,请求方式为get
 令牌:为自定义的
 消息加密密钥:为生成的

2. 定义服务器接口,代码为

@ResponseBody
    @RequestMapping("/oauth")
    public void auth(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String echostr = request.getParameter("echostr");
        if(echostr==null){
            String appId = "xxxxxxxxxxxxxx";
            String appSecret = "xxxxxxxxxxxxxxxx";
            //拼接
            String get_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
                    + "appid="
                    + appId
                    + "&secret="
                    + appSecret
                    + "&code=CODE&grant_type=authorization_code";
            String get_userinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            System.out.println("******************code=" + code);
            get_access_token_url = get_access_token_url.replace("CODE", code);
            String json = HttpsGetUtil.doHttpsGetJson(get_access_token_url);
            JSONObject jsonObject = JSONObject.fromObject(json);
            String access_token = jsonObject.getString("access_token");
            String openid = jsonObject.getString("openid");
            get_userinfo = get_userinfo.replace("ACCESS_TOKEN", access_token);
            get_userinfo = get_userinfo.replace("OPENID", openid);
            String userInfoJson = HttpsGetUtil.doHttpsGetJson(get_userinfo);
            JSONObject userInfoJO = JSONObject.fromObject(userInfoJson);
            String user_openid = userInfoJO.getString("openid");
            String user_nickname = userInfoJO.getString("nickname");
            String user_sex = userInfoJO.getString("sex");
            String user_province = userInfoJO.getString("province");
            String user_city = userInfoJO.getString("city");
            String user_country = userInfoJO.getString("country");
            String user_headimgurl = userInfoJO.getString("headimgurl");
            response.setContentType("text/html; charset=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            out.println("<HTML>");
            out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
            out.println(" <BODY>");
            out.print(" This is ");
            out.print(this.getClass());
            out.println(", using the POST method \n");
            out.println("openid:" + user_openid + "\n\n");
            out.println("nickname:" + user_nickname + "\n\n");
            out.println("sex:" + user_sex + "\n\n");
            out.println("province:" + user_province + "\n\n");
            out.println("city:" + user_city + "\n\n");
            out.println("country:" + user_country + "\n\n");
            out.println("<img src=/" + user_headimgurl + "/");
            out.println(">");
            out.println(" </BODY>");
            out.println("</HTML>");
            out.flush();
            out.close();
        }else{
            response.getWriter().write(echostr);
        }
    }

注:
appid:为公众号的开发者ID
appSecret:为公众号的开发者密码
最后,当点击提交时,提示连接成功为配置成功,至此微信公众号与自定义的后台对接成功

上一篇下一篇

猜你喜欢

热点阅读