JAVA服务端公众号获取微信授权信息

JAVA服务端公众号获取微信授权信息

注意事项

1.前提需在微信公众平台申请公众号后即可获取appid和secret两个参数
2.获取微信公众号授权信息时,后台会通过调微信服务接口获取access_token,前需在微信公众平台进行白名单设置(请求微信服务接口所在的服务IP),否则会报请求不成功
如何设置白名单链接
3.获取微信公众号授权信息时,还必须先在微信公众平台设置网页授权域名,否则请求不成功
如何设置网页授权域名链接
4.微信后台对获取access_token和ticket这两个接口每天有次数限制前且每次获取的有效期为两个小时,所以此处商户服务端需要考虑将其放至缓存中,减少请求微信接口的次数

代码展示

参数:
WECHAT_OFFICIAL_APPID:微信appid
WECHAT_OFFICIAL_SECRET:微信secret
WECHAT_OFFICIAL_REQUEST_URL:https://api.weixin.qq.com/sns/oauth2/access_token

获取微信openId

/**
     *  <desc>
     *      获取微信openId
     *  </desc>
     *
     * @param code 微信参数
     * @return
     * @createDate 2019/04/01
     */
    @RequestMapping(path = "/getOpenId",method = RequestMethod.POST)
    @ResponseBody
    public MessageVO getOpenId(String code){
        Map<String, Object> map = new HashMap<>();
        String status = "1";
        String msg = "ok";
        String requestUrl = WECHAT_OFFICIAL_REQUEST_URL +"?appid="+WECHAT_OFFICIAL_APPID+"&secret="+WECHAT_OFFICIAL_SECRET+"&code="+code+"&grant_type=authorization_code";
        try {
            if(StringUtils.isBlank(code)){
                status = "0";//失败状态
                msg = "code为空";
            }else {
            System.out.println(requestUrl);
                // 发起GET请求获取凭证
                JSONObject jsonObject = HttpProtocolUtil.httpsRequest(requestUrl, "GET", null);
                if (jsonObject != null) {
                    try {
                        map.put("openid", jsonObject.getString("openid"));
                    } catch (JSONException e) {
                        // 获取token失败
                        status = "0";
                        msg = "code无效";
                    }
                }else {
                    status = "0";
                    msg = "code无效";
                }
            }
            map.put("status", status);
            map.put("msg", msg);
        } catch (Exception e) {
            throw new DataAccessException("【微信公众号_停车场】获取openId失败",e);
        }
        return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),map);
    }

获取微信授权信息

参数:
url:前端公从号授权信息当前页面url

/**
     *  <desc>
     *      获取微信授权信息
     *  </desc>
     *
     * @return
     * @createDate 2019/04/03
     * @author Juguang.S
     */
    @RequestMapping(path = "/getTicket",method = RequestMethod.POST)
    @ResponseBody
    public MessageVO getJsapiTicket(String url) throws Exception {
        url = URLDecoder.decode(url, "UTF-8");
        Map<String, String> map = new HashMap<>();
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token"+"?appid="+WECHAT_OFFICIAL_APPID+"&secret="+WECHAT_OFFICIAL_SECRET+"&grant_type=client_credential";
        try {
            String ticket = "";
            //先从缓存中取access_token和ticket其有效期为两个小时,如取不到再调接口获取
            ticket = redisService.get(RedisConstant.WECHAT_OFFICIAL_TICKET);
            if(StringUtils.isEmpty(ticket)){
                //发起GET请求获取凭证
                JSONObject jsonObject = HttpProtocolUtil.httpsRequest(requestUrl, "GET", null);
                if (jsonObject != null) {
                    String accessToken = jsonObject.getString("access_token");
                    if(StringUtils.isNotEmpty(accessToken)){
                        redisService.set(RedisConstant.WECHAT_OFFICIAL_ACCESS_TOKEN,RedisConstant.WECHAT_OFFICIAL_TIME,accessToken);
                    }
                    String requestSecondUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket"+"?access_token="+accessToken+"&type=JSAPI";
                    JSONObject jsonObjectSecond = HttpProtocolUtil.httpsRequest(requestSecondUrl, "GET", null);
                    if(jsonObjectSecond != null){
                    ticket = jsonObjectSecond.getString("ticket");
                        if(StringUtils.isNotEmpty(ticket)){
                            redisService.set(RedisConstant.WECHAT_OFFICIAL_TICKET,RedisConstant.WECHAT_OFFICIAL_TIME,ticket);
                        }
                    }
                }
            }
            map = Sha1EncryptUtil.getSign(ticket, url);
        } catch (Exception e) {
            throw new DataAccessException("【微信公众号_停车场】获取ticket失败",e);
        }
        return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),map);
    }

猜你喜欢

转载自blog.csdn.net/songjuguang/article/details/89477867
今日推荐