1、什麼是access_tokenjava
微信公衆平臺技術文檔中對access_token的解釋:json
access_token是公衆號的全局惟一接口調用憑據,公衆號調用各接口時都需使用access_token。開發者須要進行妥善保存。access_token的存儲至少要保留512個字符空間。access_token的有效期目前爲2個小時,需定時刷新,重複獲取將致使上次獲取的access_token失效。api
技術文檔中建議服務器
1.使用中控服務器統一獲取刷新access_token,避免不一樣的業務邏輯各自刷新容易引發衝突;微信
2.access_token中有expire_in,目前是7200秒以內的值能夠根據該值控制刷新access_token,刷新過程當中中控服務器能夠繼續對外輸出老的access_token,公衆平臺後臺會保證在5分鐘內新老access_token能夠繼續使用;app
2、調用access_token請求說明微信公衆平臺
使用https請求:GETthis
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRETurl
請求成功後返回JSON數據包spa
{"access_token":"ACCESS_TOKEN","expires_in":7200}
錯誤時會返回錯誤緣由
{"errcode":40013,"errmsg":"invalid appid"}
3、實現思路
經過HttpURLConnection實現https請求,獲取到返回數據包後根據ACCESS_TOKEN保存到文本文件中供業務邏輯調用,expires_in中的時間控制刷新。
3、實現代碼
公用類HttpsUtil用來調用http鏈接
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package priv.liu.weichat.util; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.URL; import java.net.HttpURLConnection; import javax.net.ssl.HttpsURLConnection; import net.sf.json.JSONObject; /** * * @author liu */ public class HttpsUtil { /** * HttpsUtil方法https請求結果返回蔚json類型 * @param Url http請求地址 * @param Method http請求類型支持POST GET * @param Output * @return InputStream轉換成JSONObject後返回 * @throws Exception */ public JSONObject HttpsUtil(String Url,String Method,String Output) throws Exception{ JSONObject jsonObject = null; URL conn_url = new URL(Url); HttpURLConnection conn = (HttpsURLConnection)conn_url.openConnection(); conn.setRequestMethod(Method); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); conn.connect(); //output獲取access_token是不會用到 if(Output != null){ OutputStream outputstream =conn.getOutputStream(); //字符集,防止出現中文亂碼 outputstream.write(Output.getBytes("UTF-8")); outputstream.close(); } //正常返回代碼爲200 if(conn.getResponseCode()==200){ InputStream stream = conn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(stream, "utf-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; StringBuffer buffer = new StringBuffer(); while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } bufferedReader.close(); inputStreamReader.close(); stream.close(); conn.disconnect(); jsonObject = JSONObject.fromObject(buffer.toString()); } System.out.println(conn.getResponseCode()); return jsonObject; } }
循環獲取accesst_token代碼
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package priv.liu.weichat.token; import static java.lang.Thread.sleep; import net.sf.json.JSONObject; import priv.liu.weichat.util.HttpsUtil; /** * * @author liu */ public class GetToken { /** * @param args the command line arguments * @throws java.lang.Exception */ public static void main(String[] args) throws Exception { // TODO code application logic here //訪問地址 String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; //appid String APPID = "wxf62ff5631358ef2e"; //appsecret String APPSECRET = "2af33a0092006148a5000ff06e9151bb"; String request_url = TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET); HttpsUtil httpsUtil = new HttpsUtil(); System.out.println(request_url); int i = 0; while(true){ JSONObject jsonObject = httpsUtil.HttpsUtil(request_url,"GET",null); if(null != jsonObject){ String access_tocken = jsonObject.getString("access_token"); String expires_in = jsonObject.getString("expires_in"); //獲取到的access_tocken值能夠寫入到文本文件中供其餘業務邏輯調用,實例中只打印了沒有保存到文件 System.out.println("獲取成功"+"access_tocken="+access_tocken+"||expires_in="+expires_in); i=Integer.parseInt(expires_in); } //休眠1小時57分鐘,提早三分鐘獲取新的access_token sleep((7200-180)*1000); } } }