微信公眾號獲取access_token的方法例項分析
阿新 • • 發佈:2020-01-07
本文例項講述了微信公眾號獲取access_token的方法。分享給大家供大家參考,具體如下:
上一版需求做了微信公眾號開發,秀了一波操作,也遇到了很多坑。現在把微信公眾號一些基本的操作記錄一下。
微信公眾號獲取access_token 官方文件地址
access_token是公眾號的全域性唯一介面呼叫憑據,我們和微信伺服器進行互動,伺服器通過access_token判斷我們是誰(哪個公眾號服務的請求)。所以 我們在開發過程中服務端拿到的access_token是一定不能顯式暴露給外部,否則將導致資料安全問題。別人拿到你的accessToken操作你的公眾號。access_token的有效期目前為2個小時,過期需要再次獲取。
下面是一種獲取access_token方式
1.專案新增httpclient相關依賴,示例使用httpclient請求微信伺服器,獲取微信返回結果。
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.6</version> </dependency>
2.httpClientUtil類,網上隨手找的 試了一下本例的doget方法 沒有問題,其他的 暫不考慮
public class HttpClientUtil { public static String doGet(String url,Map<String,String> param) { // 建立Httpclient物件 CloseableHttpClient httpclient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse response = null; try { // 建立uri URIBuilder builder = new URIBuilder(url); if (param != null) { for (String key : param.keySet()) { builder.addParameter(key,param.get(key)); } } URI uri = builder.build(); // 建立http GET請求 HttpGet httpGet = new HttpGet(uri); // 執行請求 response = httpclient.execute(httpGet); // 判斷返回狀態是否為200 if (response.getStatusLine().getStatusCode() == 200) { resultString = EntityUtils.toString(response.getEntity(),"UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null) { response.close(); } httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; } public static String doGet(String url) { return doGet(url,null); } public static String doPost(String url,String> param) { // 建立Httpclient物件 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 建立Http Post請求 HttpPost httpPost = new HttpPost(url); // 建立引數列表 if (param != null) { List<NameValuePair> paramList = new ArrayList<>(); for (String key : param.keySet()) { paramList.add(new BasicNameValuePair(key,param.get(key))); } // 模擬表單 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8"); httpPost.setEntity(entity); } // 執行http請求 response = httpClient.execute(httpPost); resultString = EntityUtils.toString(response.getEntity(),"utf-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return resultString; } public static String doPost(String url) { return doPost(url,null); } public static String doPostJson(String url,String json) { // 建立Httpclient物件 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 建立Http Post請求 HttpPost httpPost = new HttpPost(url); // 建立請求內容 StringEntity entity = new StringEntity(json,ContentType.APPLICATION_JSON); httpPost.setEntity(entity); // 執行http請求 response = httpClient.execute(httpPost); resultString = EntityUtils.toString(response.getEntity(),"utf-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return resultString; } }
3.第三步就是簡單的測試程式碼了
public class WeChatAccessTokenTest { public static void main(String[] args) { Map<String,String> params = new HashMap<>(); // TODO: 2018/11/16 *號改成真實appid params.put("appid","******"); // TODO: 2018/11/16 *號改成真實secret params.put("secret","******"); params.put("grant_type","client_credential"); String response = HttpClientUtil.doGet("https://api.weixin.qq.com/cgi-bin/token",params); JSONObject accessTokenObject = JSONObject.parseObject(response); String accessToken = accessTokenObject.getString("access_token"); Long expire = accessTokenObject.getLong("expires_in"); System.out.println(accessToken); } }
以上就是微信公眾號基礎卻比較重要的獲取access_token操作了!
更多關於java演算法相關內容感興趣的讀者可檢視本站專題:《Java字元與字串操作技巧總結》、《Java陣列操作技巧總結》、《Java數學運算技巧總結》、《Java編碼操作技巧總結》和《Java資料結構與演算法教程》
希望本文所述對大家java程式設計有所幫助。