最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信公眾號獲取access_token的方法實例分析

 更新時間:2019年10月25日 09:14:33   作者:游語  
這篇文章主要介紹了微信公眾號獲取access_token的方法,結(jié)合實例形式分析了java實現(xiàn)微信公眾號獲取access_token的相關原理、實現(xiàn)方法及操作注意事項,需要的朋友可以參考下

本文實例講述了微信公眾號獲取access_token的方法。分享給大家供大家參考,具體如下:

上一版需求做了微信公眾號開發(fā),秀了一波操作,也遇到了很多坑。現(xiàn)在把微信公眾號一些基本的操作記錄一下。

微信公眾號獲取access_token  官方文檔地址

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

下面是一種獲取access_token方式

1.項目添加httpclient相關依賴,示例使用httpclient請求微信服務器,獲取微信返回結(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類,網(wǎng)上隨手找的 試了一下本例的doget方法 沒有問題,其他的 暫不考慮

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);
      // 執(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<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)建參數(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請求
      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<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算法相關內(nèi)容感興趣的讀者可查看本站專題:《Java字符與字符串操作技巧總結(jié)》、《Java數(shù)組操作技巧總結(jié)》、《Java數(shù)學運算技巧總結(jié)》、《Java編碼操作技巧總結(jié)》和《Java數(shù)據(jù)結(jié)構(gòu)與算法教程

希望本文所述對大家java程序設計有所幫助。

相關文章

  • IDEA maven compile報錯OutOfMemoryError(內(nèi)存溢出)解決及jvm分析

    IDEA maven compile報錯OutOfMemoryError(內(nèi)存溢出)解決及jvm分析

    遇到Maven編譯時報OutOfMemoryError錯誤通常因為默認的堆內(nèi)存大小不足,本文就來介紹一下OutOfMemoryError(內(nèi)存溢出)解決,具有一定的參考價值,感興趣的可以了解一下
    2024-10-10
  • SpringBoot分離打Jar包的兩種配置方式

    SpringBoot分離打Jar包的兩種配置方式

    這篇文章主要介紹了SpringBoot分離打Jar包的兩種配置方式,方式一是基于maven-jar-plugin,方式二是基于spring-boot-maven-plugin,文中結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • Java并發(fā)問題之樂觀鎖與悲觀鎖

    Java并發(fā)問題之樂觀鎖與悲觀鎖

    這篇文章主要介紹了Java并發(fā)問題之樂觀鎖與悲觀鎖,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • SpringBoot使用Redis對用戶IP進行接口限流的項目實踐

    SpringBoot使用Redis對用戶IP進行接口限流的項目實踐

    本文主要介紹了SpringBoot使用Redis對用戶IP進行接口限流,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • SpringBoot容器的主要組件詳解

    SpringBoot容器的主要組件詳解

    這篇文章主要介紹了SpringBoot容器的主要組件詳解,SpringBoot?是基于?Spring?Framework?的一種快速開發(fā)框架,它可以幫助開發(fā)者快速地構(gòu)建獨立的、生產(chǎn)級別的、可部署的應用程序,需要的朋友可以參考下
    2023-09-09
  • mybatis-plus雪花算法自動生成機器id原理及源碼

    mybatis-plus雪花算法自動生成機器id原理及源碼

    Mybatis-Plus是一個Mybatis的增強工具,它在Mybatis的基礎上做了增強,卻不做改變,Mybatis-Plus是為簡化開發(fā)、提高開發(fā)效率而生,但它也提供了一些很有意思的插件,比如SQL性能監(jiān)控、樂觀鎖、執(zhí)行分析等,下面一起看看mybatis-plus雪花算法自動生成機器id原理解析
    2021-06-06
  • Java 添加和刪除PDF圖層的示例代碼

    Java 添加和刪除PDF圖層的示例代碼

    本文將介紹如何使用Spire.PDF for Java來添加和刪除PDF圖層,本文通過示例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-02-02
  • java動態(tài)代理實現(xiàn)代碼

    java動態(tài)代理實現(xiàn)代碼

    這篇文章主要介紹了java 動態(tài)代理的的相關資料,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下,希望能給你帶來幫助
    2021-07-07
  • 如何基于sqlite實現(xiàn)kafka延時消息詳解

    如何基于sqlite實現(xiàn)kafka延時消息詳解

    這篇文章主要給大家介紹了關于如何基于sqlite實現(xiàn)kafka延時消息的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2022-01-01
  • Mybatis的Dao層實現(xiàn)原理分析

    Mybatis的Dao層實現(xiàn)原理分析

    這篇文章主要介紹了Mybatis的Dao層實現(xiàn)原理分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12

最新評論

高陵县| 岳西县| 洪泽县| 南宫市| 武汉市| 合江县| 公安县| 清丰县| 佛学| 沈阳市| 信丰县| 海阳市| 祁东县| 普格县| 大余县| 大田县| 教育| 思茅市| 涞水县| 曲阳县| 清镇市| 临澧县| 承德县| 揭东县| 大港区| 汝城县| 前郭尔| 米泉市| 枣庄市| 都匀市| 彭州市| 乌鲁木齐市| 运城市| 通山县| 荣昌县| 油尖旺区| 鲁甸县| 丹棱县| 沾益县| 黔南| 丰都县|