服务端生成小程序二维码、扫码可直接进小程序

      分为两步、要生成小程序的二维码,都是调用小程序自己的api

      1、先获取token

      2、然后再生成二维码

    官方文档https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html

             HttpGet httpGet = new HttpGet(
                        "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                                + appid + "&secret=" + secret );
                HttpClient httpClient = HttpClients.createDefault();
                HttpResponse res = httpClient.execute(httpGet);
                HttpEntity entity = res.getEntity();
                String result = EntityUtils.toString(entity, "UTF-8");
                JSONObject jsons = JSONObject.fromObject(result);
                String expires_in = jsons.getString("expires_in");
                
                //缓存
                if(Integer.parseInt(expires_in)==7200){
                    //ok
                    String access_token = jsons.getString("access_token");
                    System.out.println("access_token:"+access_token);
}
                URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("POST");// 提交模式
                    // 发送POST请求必须设置如下两行
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);
                    // 获取URLConnection对象对应的输出流
                    PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
                 // 发送请求参数
                    JSONObject paramJson = new JSONObject();
                    paramJson.put("scene", "a=1234567890");
                    paramJson.put("page", page);
                    paramJson.put("width", 430);
                    paramJson.put("auto_color", true);
                    printWriter.write(paramJson.toString());
                    // flush输出流的缓冲
                    printWriter.flush();
                    //开始获取数据
                     bis = new BufferedInputStream(httpURLConnection.getInputStream());
                     bos = new ByteArrayOutputStream();
                    bos.write(bis);
                     byte[]  bs = bos.toByteArray();
                     printWriter.close();
  bos.close();
bis.close();
原文地址:https://www.cnblogs.com/qq376324789/p/11023104.html