第一步:准備工作:准備小程序的APPID和SECRET
可登陸微信公眾平台查看APPID和secret
第二部:獲取token
微信提供了獲取token的接口,訪問此接口同時傳入APPID和secret作為參數,就能獲取token值。
接口地址為:tokenURL = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET
Java代碼:
1 public static String getAccessToken() throws UnsupportedEncodingException, IOException{ 2 String tokenURL = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET 3 JSONObject jsonObject = null; 4 URL conn_url = new URL(tokenURL ); 5 HttpURLConnection conn = (HttpsURLConnection)conn_url.openConnection(); 6 conn.setRequestMethod("GET"); 7 conn.setReadTimeout(5000); 8 conn.setConnectTimeout(5000); 9 conn.connect(); 10 //output獲取access_token是不會用到 11 12 //正常返回代碼為200 13 if(conn.getResponseCode()==200){ 14 InputStream stream = conn.getInputStream(); 15 InputStreamReader inputStreamReader = new InputStreamReader(stream, "utf-8"); 16 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 17 String str = null; 18 StringBuffer buffer = new StringBuffer(); 19 while ((str = bufferedReader.readLine()) != null) { 20 buffer.append(str); 21 } 22 bufferedReader.close(); 23 inputStreamReader.close(); 24 stream.close(); 25 conn.disconnect(); 26 jsonObject = JSONObject.fromObject(buffer.toString()); 27 } 28 29 String access_token = jsonObject.getString("access_token"); 30 return access_token; 31 }
第三部:獲取參數圖片
微信官方提供了獲取微信小程序二維碼的接口,訪問此接口,同時把第二步獲取的token值作為參數傳入,同時還需傳入二維碼內需要帶的參數,和圖片的一些設置參數,便可獲取二維碼圖片的輸入流。
接口地址:
https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=access_token
基本的設置參數:
"scene", param --二維碼內需要帶的參數param
"path", "pages/ShouYe/ShouYe/ShouYe" --設置掃面二維碼進入的頁面
"width", width --設置二維碼圖片的大小 如果不設置,默認大小是430
"is_hyaline", true
"auto_color", true
生成二維碼代碼:
1 public static String getQRImg(String param,String size, HttpServletRequest request) { 2 3 4 String p=request.getSession().getServletContext().getRealPath("/"); 5 String codeUrl = p+"image/codeImg/"+System.currentTimeMillis()+param + "Code.png"; 6 String twoCodeUrl="/image/codeImg/"+System.currentTimeMillis()+param + "Code.png"; 7 try 8 { 9 String access_token = token; 10 URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token); 11 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 12 httpURLConnection.setRequestMethod("POST");// 提交模式 13 // conn.setConnectTimeout(10000);//連接超時 單位毫秒 14 // conn.setReadTimeout(2000);//讀取超時 單位毫秒 15 // 發送POST請求必須設置如下兩行 16 httpURLConnection.setDoOutput(true); 17 httpURLConnection.setDoInput(true); 18 // 獲取URLConnection對象對應的輸出流 19 PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); 20 // 發送請求參數 21 JSONObject paramJson = new JSONObject(); 22 String width; 23 if(size !=null && !size.trim().equals("")){ 24 if(size.trim().matches("^[1-9][0-9]*$")){ 25 width = size.trim(); 26 }else 27 { 28 width = "430"; 29 } 30 }else{ 31 width = "430"; 32 } 33 paramJson.put("scene", param); 34 paramJson.put("path", "pages/ShouYe/ShouYe/ShouYe"); 35 paramJson.put("width", width); 36 paramJson.put("is_hyaline", true); 37 paramJson.put("auto_color", true); 38 /** 39 * line_color生效 40 * paramJson.put("auto_color", false); 41 * JSONObject lineColor = new JSONObject(); 42 * lineColor.put("r", 0); 43 * lineColor.put("g", 0); 44 * lineColor.put("b", 0); 45 * paramJson.put("line_color", lineColor); 46 * */ 47 48 printWriter.write(paramJson.toString()); 49 // flush輸出流的緩沖 50 printWriter.flush(); 51 //開始獲取數據 52 BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream()); 53 OutputStream os = new FileOutputStream(new File(codeUrl)); 54 int len; 55 byte[] arr = new byte[1024]; 56 while ((len = bis.read(arr)) != -1) 57 { 58 os.write(arr, 0, len); 59 os.flush(); 60 } 61 os.close(); 62 } 63 catch (Exception e) 64 { 65 e.printStackTrace(); 66 } 67 return twoCodeUrl; 68 }
