欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

请求微信接口获取小程序码

程序员文章站 2022-06-12 20:05:50
...
public Map<String,Object> getSharePhoto(Map<String, Object> conditions){
        Map<String, Object> results = new HashMap<String, Object>();
        results.put("success",false);
        String userId = "123456464";
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpClient client = new DefaultHttpClient();
        try {
            //获取小程序token
            Map params = new HashMap();
            params.put("appid","appid");//小程序appid
            params.put("secret","secret");//小程序secret
            params.put("grant_type","client_credential");
            String returnMsg = null;
            String token = null;
            returnMsg = WhatyHttpClient.doGet("https://api.weixin.qq.com/cgi-bin/token", params);
            net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(returnMsg);
            if (jsonObject.get("access_token") != null){
                token = jsonObject.get("access_token").toString();
            }else {
                results.put("success",false);
                results.put("info","获取小程序access_token失败");
            }
            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token;
            HttpPost post = new HttpPost(url);
            JSONObject json = new JSONObject();
            json.put("scene",userId);
            json.put("is_hyaline",true); //透明小程序码
            //json.put("page","pages/test/test");
            post.setHeader("Content-Type", "application/json");
            post.addHeader("Authorization", "Basic YWRtaW46");
            StringEntity s = new StringEntity(json.toString(), "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            post.setEntity(s);
            // 发送请求
            HttpResponse httpResponse = client.execute(post);
            // 获取响应输入流
            InputStream inStream = httpResponse.getEntity().getContent();
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String photoLink = null; // 文件上传目录
                photoLink = Const.FILE_PATH + userSession.getId() + ".png"; //整个路径加文件名
                File savedFile = new File(ServletActionContext.getServletContext().getRealPath(Const.FILE_PATH) + "/" + userSession.getId()
                        + ".png");
                FileOutputStream fileOut = new FileOutputStream(savedFile);
                BufferedOutputStream dataOut=new BufferedOutputStream (fileOut);
                ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
                byte[] buff = new byte[100];
                int rc = 0;
                while ((rc = inStream.read(buff, 0, 100)) > 0) {
                    swapStream.write(buff, 0, rc);
                }
                byte[] in2b = swapStream.toByteArray();
                dataOut.write(in2b);
                dataOut.flush();
                inStream.close();
                dataOut.close();
                results.put("success",true);
                results.put("photoLink",photoLink);
            } else {
                results.put("info","请求服务器失败");
            }
        }catch (Exception e){
            results.put("success",false);
            results.put("info","获取小程序码失败");
            e.printStackTrace();
            return results;
        }
        return results;
    }
相关标签: 微信