博主首页

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

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

 

posted @   笑~笑  阅读(2550)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示