1. 程式人生 > >請求微信介面獲取小程式碼

請求微信介面獲取小程式碼

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;
    }