- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- 微信公眾號怎么獲取access_token
這篇文章主要為大家展示了“微信公眾號怎么獲取access_token”,內容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習一下“微信公眾號怎么獲取access_token”這篇文章吧。
access_token是公眾號的全局唯一接口調用憑據,我們和微信進(jìn)行交互,服務(wù)器通過(guò)access_token判斷我們是誰(shuí)(哪個(gè)公眾號服務(wù)的請求)。所以 我們在開(kāi)發(fā)過(guò)程中服務(wù)端拿到的access_token是一定不能顯式暴露給外部,否則將導致數據安全問(wèn)題。別人拿到你的accessToken操作你的公眾號。access_token的有效期目前為2個(gè)小時(shí),過(guò)期需要再次獲取。
下面是一種獲取access_token方式
1.項目添加httpclient相關(guān)依賴(lài),示例使用httpclient請求微信服務(wù)器,獲取微信返回結果。
<!-- 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類(lèi),網(wǎng)上隨手找的 試了一下本例的doget方法 沒(méi)有問(wèn)題,其他的 暫不考慮
public class HttpClientUtil { public static String doGet(String url, Map<String, String> param) { // 創(chuàng )建Httpclient對象 CloseableHttpClient httpclient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse response = null; try { // 創(chuàng )建uri URIBuilder builder = new URIBuilder(url); if (param != null) { for (String key : param.keySet()) { builder.addParameter(key, param.get(key)); } } URI uri = builder.build(); // 創(chuàng )建http GET請求 HttpGet httpGet = new HttpGet(uri); // 執行請求 response = httpclient.execute(httpGet); // 判斷返回狀態(tài)是否為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, Map<String, String> param) { // 創(chuàng )建Httpclient對象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 創(chuàng )建Http Post請求 HttpPost httpPost = new HttpPost(url); // 創(chuàng )建參數列表 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) { // 創(chuàng )建Httpclient對象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 創(chuàng )建Http Post請求 HttpPost httpPost = new HttpPost(url); // 創(chuàng )建請求內容 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.第三步就是簡(jiǎn)單的測試代碼了
public class WeChatAccessTokenTest { public static void main(String[] args) { Map<String, String> params = new HashMap<>(); // TODO: 2018/11/16 *號改成真實(shí)appid params.put("appid", "******"); // TODO: 2018/11/16 *號改成真實(shí)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); }}
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站