本方法主要通过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 }
时间: 2024-11-10 06:29:27