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

在java中获取小程序二维码时遇见了很多坑,这里记一下。

	//配置常量 (如果确定不变就放在常量里,推荐放在配置文件中 application.yml)
	// appid
	private static final String APP_ID = "";
	// appsecret
    private static final String APP_SECRET = "";
	// 获取acc_token的请求地址
	private static final String GET_TOKEN_URL= "https://api.weixin.qq.com/cgi-bin/token";
	// 获取图片的地址:createwxaqrcode、getwxacodeunlimit、getwxacode;按需求选调用接口
	private static final String GET_QR_URL= "https://api.weixin.qq.com/wxa/getwxacode";

  	public String getQr(String code) throws IOException {
  		/**
  		 * 坑1 在使用cloud之后不能使用注解形式将RestTemplate注入(Resource、Autowired)
  		 * @Resource
  		 * RestTemplate restTemplate
  		 * 注入RestTemplate会请求内部cloud模块,报错没有注入该api.weixin.qq模块
  		 */
		RestTemplate restTemplate = new RestTemplate();
		String template = restTemplate.getForObject(GET_TOKEN_URL + "?grant_type=client_credential&appid=" + APP_ID + "&secret=" + APP_SECRET, String.class);
        HashMap hashMap = JSON.parseObject(template, HashMap.class);
        String accessToken = (String) hashMap.get("access_token");
        //配置参数
		 Map<String, Object> param = new HashMap<>();
         param.put("path", "pages/index/index?code=" + code);
         param.put("auto_color", false);
         param.put("width", 298);
         Map lineColor = new HashMap();
         lineColor.put("r", 0);
         lineColor.put("g", 0);
         lineColor.put("b", 0);
         param.put("line_color", lineColor);
         HttpHeaders headers = new HttpHeaders();
         headers.set("Accept", "application/json");
         // 坑2 一定要将请求参数转换成string类型,否则折磨你的就是"errcode":47001,"errmsg":"data format error
HttpEntity requestEntity = new HttpEntity(JSON.toJSONString(param), headers);
        ResponseEntity<byte[]> entity = rest.exchange(GET_QR_URL +"?access_token="+accessToken , HttpMethod.POST, requestEntity, byte[].class);   
        byte[] result = entity.getBody();
         /**
         * 生成base6返回
         * String s = Base64.getEncoder().encodeToString(result);
         * return s;
         */ 
        //文件流输出
        InputStream inputStream = new ByteArrayInputStream(result);
        //文件名文件地址,自动生成
        File file = new File("C:/image/qr/"+code+".png");
        if (!file.exists()) {
            file.createNewFile();
         }
         OutputStream outputStream = new FileOutputStream(file);
         int len = 0;
         byte[] buf = new byte[1024];
         while ((len = inputStream.read(buf, 0, 1024)) != -1) {
             outputStream.write(buf, 0, len);
         }
         outputStream.flush();
         //文件名文件地址,自动生成
         return "http://127.0.0.1/image/qr/"+code+".png";
	}

posmain获取acc_token
在这里插入图片描述
posmain获取程序码
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vace cc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值