编程语言
首页 > 编程语言> > springboot通过code获取微信小程序UnionID

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

作者:互联网

前提:将微信小程序绑定到微信开放平台帐号 !!!

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

        ResponseEntity<String> res = restTemplate.getForEntity(url, String.class);
        JSONObject jsonObject = JSONObject.parseObject(res.getBody());
        System.out.println("jsonObject============="+jsonObject);
        remap.put("unionId",jsonObject.get("unionid").toString());
        return ResultUtil.success(remap);
}

标签:Map,remap,code,springboot,微信,jsonObject,JSONObject
来源: https://blog.csdn.net/qq_38410795/article/details/122876824