springboot通过code获取微信小程序openId

    @RequestMapping("/getOpenId")
    public Result wx(@RequestBody Map map) {
        Map remap = new HashMap<>();
        //1、向微信服务器 使用登录凭证 code 获取 session_key 和 openid
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxAppID +
                "&secret=" + secret +
                "&js_code=" + map.get("code").toString() +
                "&grant_type=authorization_code";

        ResponseEntity res = restTemplate.getForEntity(url, String.class);
        JSONObject jsonObject = JSONObject.parseObject(res.getBody());
        remap.put("openId",jsonObject.get("openid").toString());
        return ResultUtil.success(remap);
    }

你可能感兴趣的:(微信开发,springboot,微信小程序)