public static String getAccessToken() throws ClientProtocolException, IOException { String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"; url = url + "&appid=" + 你的公衆平臺開發appId + "&secret=" + 你的公衆平臺appSecret; DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse; httpResponse = client.execute(httpGet); int code = httpResponse.getStatusLine().getStatusCode(); String strResult = EntityUtils.toString(httpResponse.getEntity(),WxData.CharSet); if (code == 200) { JSONObject jsonObject = JSONObject.fromObject(strResult); String token = jsonObject.getString("access_token"); System.out.println(token); } return null; }
天天接口訪問次數爲2000次,因此要保存到全局變量中,7200s的技能持續時間;java
所須要的jar包json
commons-beanutils
commons-collections
commons-httpclient
commons-lang
commons-logging
json-lib-2.3-jdk15
httpcore-4.2.2
httpmime-4.2.3
httpclient-cache-4.2.3
httpclient-4.2.3
api