一、什么是access_token
微信公眾平台技術文檔中對access_token的解釋:
access_token是公眾號的全局唯一接口調用憑據,公眾號調用各接口時都需使用access_token。開發者需要進行妥善保存。access_token的存儲至少要保留512個字符空間。access_token的有效期目前為2個小時,需定時刷新,重復獲取將導致上次獲取的access_token失效。
技術文檔中建議
1.使用中控服務器統一獲取刷新access_token,避免不同的業務邏輯各自刷新容易引起沖突;
2.access_token中有expire_in,目前是7200秒之內的值可以根據該值控制刷新access_token,刷新過程中中控服務器可以繼續對外輸出老的access_token,公眾平台后台會保證在5分鍾內新老access_token可以繼續使用;
二、調用access_token請求說明
使用https請求:GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
請求成功后返回JSON數據包
{"access_token":"ACCESS_TOKEN","expires_in":7200}
錯誤時會返回錯誤原因
{"errcode":40013,"errmsg":"invalid appid"}
三、實現思路
通過HttpURLConnection實現https請求,獲取到返回數據包后根據ACCESS_TOKEN保存到文本文件中供業務邏輯調用,expires_in中的時間控制刷新。
三、實現代碼
公用類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); } } }