這篇文章主要為大家展示了“微信公眾號(hào)怎么獲取access_token”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“微信公眾號(hào)怎么獲取access_token”這篇文章吧。
創(chuàng)新互聯(lián)建站是一家專(zhuān)注于成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)與策劃設(shè)計(jì),阿合奇網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:阿合奇等地區(qū)。阿合奇做網(wǎng)站價(jià)格咨詢(xún):13518219792access_token是公眾號(hào)的全局接口調(diào)用憑據(jù),我們和微信服務(wù)器進(jìn)行交互,服務(wù)器通過(guò)access_token判斷我們是誰(shuí)(哪個(gè)公眾號(hào)服務(wù)的請(qǐng)求)。所以 我們?cè)陂_(kāi)發(fā)過(guò)程中服務(wù)端拿到的access_token是一定不能顯式暴露給外部,否則將導(dǎo)致數(shù)據(jù)安全問(wèn)題。別人拿到你的accessToken操作你的公眾號(hào)。access_token的有效期目前為2個(gè)小時(shí),過(guò)期需要再次獲取。
下面是一種獲取access_token方式
1.項(xiàng)目添加httpclient相關(guān)依賴(lài),示例使用httpclient請(qǐng)求微信服務(wù)器,獲取微信返回結(jié)果。
<!-- 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對(duì)象 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請(qǐng)求 HttpGet httpGet = new HttpGet(uri); // 執(zhí)行請(qǐng)求 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對(duì)象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 創(chuàng)建Http Post請(qǐng)求 HttpPost httpPost = new HttpPost(url); // 創(chuàng)建參數(shù)列表 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); } // 執(zhí)行http請(qǐng)求 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對(duì)象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 創(chuàng)建Http Post請(qǐng)求 HttpPost httpPost = new HttpPost(url); // 創(chuàng)建請(qǐng)求內(nèi)容 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); httpPost.setEntity(entity); // 執(zhí)行http請(qǐng)求 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)單的測(cè)試代碼了
public class WeChatAccessTokenTest { public static void main(String[] args) { Map<String, String> params = new HashMap<>(); // TODO: 2018/11/16 *號(hào)改成真實(shí)appid params.put("appid", "******"); // TODO: 2018/11/16 *號(hào)改成真實(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); }}
以上是“微信公眾號(hào)怎么獲取access_token”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享標(biāo)題:微信公眾號(hào)怎么獲取access_token-創(chuàng)新互聯(lián)
標(biāo)題來(lái)源:http://vcdvsql.cn/article44/jgdee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、企業(yè)網(wǎng)站制作、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容