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

      分为两步、要生成小程序的二维码,都是调用小程序自己的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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值