微信公眾號(hào)獲取access_token的方法實(shí)例分析
本文實(shí)例講述了微信公眾號(hào)獲取access_token的方法。分享給大家供大家參考,具體如下:
上一版需求做了微信公眾號(hào)開(kāi)發(fā),秀了一波操作,也遇到了很多坑。現(xiàn)在把微信公眾號(hào)一些基本的操作記錄一下。
微信公眾號(hào)獲取access_token 官方文檔地址
access_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)基礎(chǔ)卻比較重要的獲取access_token操作了!
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Java字符與字符串操作技巧總結(jié)》、《Java數(shù)組操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java編碼操作技巧總結(jié)》和《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- java獲取微信accessToken的方法
- 微信公眾號(hào)平臺(tái)接口開(kāi)發(fā) 獲取access_token過(guò)程解析
- 微信公眾平臺(tái)獲取access_token的方法步驟
- 詳解微信開(kāi)發(fā)之a(chǎn)ccess_token之坑
- php獲取微信基礎(chǔ)接口憑證Access_token
- 微信小程序登錄換取token的教程
- 微信小程序url與token設(shè)置詳解
- Java微信公眾平臺(tái)開(kāi)發(fā)(6) 微信開(kāi)發(fā)中的token獲取
- 基于thinkPHP3.2實(shí)現(xiàn)微信接入及查詢(xún)token值的方法
- 微信公眾號(hào)服務(wù)器驗(yàn)證Token步驟圖解
相關(guān)文章
IDEA maven compile報(bào)錯(cuò)OutOfMemoryError(內(nèi)存溢出)解決及jvm分析
遇到Maven編譯時(shí)報(bào)OutOfMemoryError錯(cuò)誤通常因?yàn)槟J(rèn)的堆內(nèi)存大小不足,本文就來(lái)介紹一下OutOfMemoryError(內(nèi)存溢出)解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-10-10
SpringBoot使用Redis對(duì)用戶(hù)IP進(jìn)行接口限流的項(xiàng)目實(shí)踐
本文主要介紹了SpringBoot使用Redis對(duì)用戶(hù)IP進(jìn)行接口限流,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
mybatis-plus雪花算法自動(dòng)生成機(jī)器id原理及源碼
Mybatis-Plus是一個(gè)Mybatis的增強(qiáng)工具,它在Mybatis的基礎(chǔ)上做了增強(qiáng),卻不做改變,Mybatis-Plus是為簡(jiǎn)化開(kāi)發(fā)、提高開(kāi)發(fā)效率而生,但它也提供了一些很有意思的插件,比如SQL性能監(jiān)控、樂(lè)觀鎖、執(zhí)行分析等,下面一起看看mybatis-plus雪花算法自動(dòng)生成機(jī)器id原理解析2021-06-06
java動(dòng)態(tài)代理實(shí)現(xiàn)代碼
這篇文章主要介紹了java 動(dòng)態(tài)代理的的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下,希望能給你帶來(lái)幫助2021-07-07
如何基于sqlite實(shí)現(xiàn)kafka延時(shí)消息詳解
這篇文章主要給大家介紹了關(guān)于如何基于sqlite實(shí)現(xiàn)kafka延時(shí)消息的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01

