微信公众号开发自定义菜单跳转页面

微信公众号开发:

自定义菜单:静默授权跳转服务器上的url
https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=http://www.aaa.com/bbb&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect
返回回调函数http://www.aaa.com/bbb?code=code&state=123调用实际需要调用的接口,获取code后,调用
https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=code&grant_type=authorization_code
获取用户access_token和openid

例如

@RequestMapping(value = "toadd", produces = "text/plain; charset=UTF-8")
    public String toadd(@RequestParam(value = "code", required = true) String code,@RequestParam(value = "state", required = true) String state,HttpServletRequest request){
        String response = "";
          String openidurl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code="+code+"&grant_type=authorization_code";
        String str = HttpRequestUrl.sendPost(openidurl,"");//post发送
        if("error".equals(str)){
            logger.error("url error");
        }else{
            //解析字符串
            JSONObject jObj = JSONObject.fromObject(str);
            String access_token = String.valueOf(jObj.get("access_token"));
            String openid = String.valueOf(jObj.get("openid"));
            request.setAttribute("openid",openid);
        }
        return "aa/aa/aa-zc";
    }

 

 

微信公众号点击自助菜单后,跳转到服务器上的页面,点击页面上按钮时,ajax调用失败,原因是因为需要在调用ajax时加上

contentType : "application/x-www-form-urlencoded; charset=utf-8",

展开阅读全文

没有更多推荐了,返回首页