標籤:system 字串 put url string nts close weixin bytes
本方法主要通過java後台控制來擷取Access_token,需要你已經知道自己的ID跟密碼
因為的使用權限設定大概每天可以擷取兩千條,每條有效時間為2小時
1 /** 2 * 輸入自己的id跟密碼,擷取的安全密令字串 3 * @param APP_ID 4 * @param APPSECRET 5 * @return 6 */ 7 public static String getAccess_token( String APP_ID,String APPSECRET) { 8 //設定變數 url與傳回值其中url使用拼接帶入參數APP_ID, APPSECRET 9 String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="10 + APP_ID+ "&secret=" + APPSECRET;11 String accessToken = null;12 try {13 //設定連結14 URL urlGet = new URL(url);15 //設定外網代理連結16 InetSocketAddress addr = new InetSocketAddress("192.168.99.100",80); 17 Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); 18 //啟動連結19 HttpURLConnection http = (HttpURLConnection) urlGet .openConnection(proxy);20 //設定連結參數與要求21 http.setRequestMethod("GET"); // 必須是get方式請求22 http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");23 http.setDoOutput(true);24 http.setDoInput(true);25 System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 連線逾時30??26 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 讀取逾時30??27 // 連結28 http.connect();29 //擷取傳回值json位元組流30 InputStream is = http.getInputStream();31 int size = is.available();32 byte[] jsonBytes = new byte[size];33 is.read(jsonBytes);34 //轉化成字串35 String message = new String(jsonBytes, "UTF-8");36 // 轉化成json對象然後返回accessToken屬性的值37 JSONObject demoJson =JSONObject.fromObject(message);38 accessToken = demoJson.getString("access_token");39 System.out.println(accessToken);40 is.close();41 } catch (Exception e) {42 e.printStackTrace();43 }44 return accessToken;45 }46 47 }
java後台擷取Access_token的工具方法