RestTemplate+获取微信小程序二维码

获取微信小程序二维码

获取AccessToken

   public String getAccessToken(String appid, String secret) {
        Proxy proxy = null;
        if (proxyEnabled) {
            SocketAddress address = new InetSocketAddress(proxyHost, proxyPort);
            proxy = new Proxy(Proxy.Type.HTTP, address);
        }
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
        Map<String, Object> params = new HashMap<>();
        HttpHeaders headers = new HttpHeaders();
        logger.info("url:" + url);
        logger.info("参数" + params.toString());
        ResponseEntity<String> responseEntity = httpUtil.getRequest(url, params, headers, proxy);
        logger.info("返回参数" + responseEntity.toString());
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new ApiException("获取【{0}】token异常HttpStatus:{1}", responseEntity.getStatusCode());
        }
        return responseEntity.getBody();
    }

获取微信小程序二维码

RestTemplate restTemplate = new RestTemplate();
        Proxy proxy = null;
        if (proxyEnabled) {
            SocketAddress address = new InetSocketAddress(proxyHost, proxyPort);
            proxy = new Proxy(Proxy.Type.HTTP, address);
        }
        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
        Map<String, Object> params = new HashMap<>();
        params.put("scene", scene);
        params.put("page", page);
        params.put("width", 430);
        HttpHeaders headers = new HttpHeaders();
        logger.info("url:" + url);
        logger.info("参数:" + params.toString());
        ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(url, params, byte[].class);
        if (responseEntity.getStatusCode() == HttpStatus.OK) {
            byte[] body = responseEntity.getBody();
            InputStream inputStream = new ByteArrayInputStream(body);
            // 将获取流转为base64格式
            String result = "";
            byte[] data = null;
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[100];
            int rc = 0;
            while ((rc = inputStream.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            data = swapStream.toByteArray();
            result = new String(Base64.getEncoder().encode(data));
            logger.info("base64:" + result);
            inputStream.close();
            swapStream.close();
            return result;
        }
        throw new ApiException("生成二维码失败");
    }

开发过程中遇到的坑:

  • 小程序返回的是buffer。
  • 开始用string去接收再把string转成byte[]数组,发现拿到的数据转成base64解析不了。
  • 用byte[]数组接收拿到的数据转成base64可以转换成图片。
    拿到的base64字符串找一个base64转图片的在线工具试一下即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值