最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
微信公眾號怎么獲取access_token-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“微信公眾號怎么獲取access_token”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“微信公眾號怎么獲取access_token”這篇文章吧。

創(chuàng)新互聯(lián)建站是一家專注于成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)與策劃設(shè)計,阿合奇網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:阿合奇等地區(qū)。阿合奇做網(wǎng)站價格咨詢:13518219792

access_token是公眾號的全局接口調(diào)用憑據(jù),我們和微信服務(wù)器進行交互,服務(wù)器通過access_token判斷我們是誰(哪個公眾號服務(wù)的請求)。所以 我們在開發(fā)過程中服務(wù)端拿到的access_token是一定不能顯式暴露給外部,否則將導(dǎo)致數(shù)據(jù)安全問題。別人拿到你的accessToken操作你的公眾號。access_token的有效期目前為2個小時,過期需要再次獲取。

下面是一種獲取access_token方式

1.項目添加httpclient相關(guān)依賴,示例使用httpclient請求微信服務(wù)器,獲取微信返回結(jié)果。

    org.apache.httpcomponents   httpclient   4.5.3         org.apache.httpcomponents   httpcore   4.4.6  

2.httpClientUtil類,網(wǎng)上隨手找的 試了一下本例的doget方法 沒有問題,其他的 暫不考慮

public class HttpClientUtil {  public static String doGet(String url, Map 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);      // 執(zhí)行請求      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 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)建參數(shù)列表      if (param != null) {        List 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請求      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)建請求內(nèi)容      StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);      httpPost.setEntity(entity);      // 執(zhí)行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 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”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


本文名稱:微信公眾號怎么獲取access_token-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://fisionsoft.com.cn/article/jgdee.html