微信如何獲取以及保存接口調(diào)用憑證,下面就為大家進(jìn)行介紹
一、說明
*詳細(xì)說明請(qǐng)參考前兩篇文章。
*本文分為三部分:
接口調(diào)用憑證access_token的作用以及解釋
如何獲取接口調(diào)用憑證access_token
如何實(shí)現(xiàn)微信文檔所說的“中控服務(wù)器”的實(shí)現(xiàn)以保存access_token
* 本文結(jié)束會(huì)給出包括本文前三篇文章的所有演示源碼
為什么要獲取和保存接口調(diào)用憑證access_token
•開始開發(fā)-獲取接口調(diào)用憑據(jù)
?文檔地址:http://mp.weixin.qq.com/wiki/14/9f9c82c1af308e3b14ba9b973f99a8ba.html
•官網(wǎng)文檔給出這樣解釋:
?access_token是公眾號(hào)的全局唯一票據(jù),公眾號(hào)調(diào)用各接口時(shí)都需使用access_token。開發(fā)者需要進(jìn)行妥善保存。access_token的存儲(chǔ)至少要保留512個(gè)字符空間。access_token的有效期目前為2個(gè)小時(shí),需定時(shí)刷新,重復(fù)獲取將導(dǎo)致上次獲取的access_token失效。
•理解:
?我們簡(jiǎn)單翻閱文檔可以發(fā)現(xiàn),許多高級(jí)功能,例如:自定義菜單、素材管理、用戶管理、賬號(hào)管理等各種高級(jí)功能請(qǐng)求的鏈接中都有“?access_token=TOKEN”這個(gè)參數(shù),這是全局調(diào)用參數(shù),微信后臺(tái)需要根據(jù)這個(gè)參數(shù)確定身份,保證我們的微信公眾號(hào)的安全。
?為了防止公眾號(hào)的程序錯(cuò)誤而引發(fā)微信服務(wù)器負(fù)載異常,默認(rèn)情況下,每個(gè)公眾號(hào)調(diào)用接口都不能超過一定限制,這里微信限制每天2000次。所以,如果我們想要頻繁調(diào)用這個(gè)參數(shù),需要我們開發(fā)者手動(dòng)保存,每個(gè)access_token有效期是2個(gè)小時(shí)。
獲取接口調(diào)用憑證access_token
•官網(wǎng)文檔給出這樣解釋:
?接口調(diào)用請(qǐng)求說明
http請(qǐng)求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
■返回說明
正常情況下,微信會(huì)返回下述JSON數(shù)據(jù)包給公眾號(hào):
{“access_token”:”ACCESS_TOKEN”,”expires_in”:7200}
錯(cuò)誤時(shí)微信會(huì)返回錯(cuò)誤碼等信息,JSON數(shù)據(jù)包示例如下(該示例為AppID無效錯(cuò)誤):
{“errcode”:40013,”errmsg”:”invalid appid”}
•理解:
?GET請(qǐng)求,該方式直接在本地就可以實(shí)現(xiàn)。因?yàn)橹皇且粋€(gè)普通的GET請(qǐng)求,類似于訪問網(wǎng)址。所以,不需要上傳該部分代碼到服務(wù)器就可以直接操作。
?http請(qǐng)求協(xié)議是GET請(qǐng)求,說明我們需要通過GET請(qǐng)求獲取返回流,返回流是json形式。調(diào)用時(shí)我們需要攜帶三個(gè)參數(shù):grant_type、appid、secret。其中appid和secret是我們微信公眾號(hào)關(guān)鍵的參數(shù),在前文已經(jīng)闡述。返回結(jié)果分為正確和錯(cuò)誤兩種結(jié)果。【百度:json】
?其實(shí),我們可以直接在地址欄輸入文檔給的示例:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET,我們會(huì)看到這樣的信息:“{“errcode”:40013,”errmsg”:”invalid appid hint: [pQKl0120ic11]”}”,因?yàn)檫@是一個(gè)無效的請(qǐng)求,返回的是錯(cuò)誤的結(jié)果。
?當(dāng)我們把自己的測(cè)試號(hào)APPID和APPSECRET替換上面的那兩個(gè)參數(shù),會(huì)看到這樣的信息:“{“access_token”:”XrllR3fNf…bADAMIO”,”expires_in”:7200}”,代表獲取成功。
?現(xiàn)在我們通過java 代碼獲取返回流,拿取access_token。
•實(shí)現(xiàn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
private static final long MAX_TIME = 7200 * 1000 ; // 微信允許最長(zhǎng)Access_token有效時(shí)間(ms) private static final String TAG = "WeixinApiTest" ; // TAG private static final String APPID = "wx889b****b3666b0b8" ; // APPID private static final String SECERT = "6da7676***f0a9f15fbf06027856bb" ; // 秘鑰 /* * 該測(cè)試用例演示了如何獲取access_token。 * access_token是公眾號(hào)的全局唯一票據(jù),公眾號(hào)調(diào)用各接口時(shí)都需使用access_token。 */ @Test public void getAccess_token() throws IOException { // 拼接api要求的httpsurl鏈接 String urlString = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECERT; // 創(chuàng)建一個(gè)url URL reqURL = new URL(urlString); // 拿取鏈接 HttpsURLConnection httpsConn = (HttpsURLConnection) reqURL .openConnection(); // 取得該連接的輸入流,以讀取響應(yīng)內(nèi)容 InputStreamReader isr = new InputStreamReader( httpsConn.getInputStream()); // 讀取服務(wù)器的響應(yīng)內(nèi)容并顯示 char[] chars = new char[1024]; String reslut = ""; int len; while ((len = isr.read(chars)) != -1) { reslut += new String(chars, 0, len); } isr.close(); /* * 轉(zhuǎn)化json成javabean。引入了第三方j(luò)ar:GSON */ Gson gson = new Gson(); // 將獲取的json轉(zhuǎn)化為java中的bean // 注意:Access_token access_token是一個(gè)自己創(chuàng)建的javabean Access_token access_token = gson.fromJson(reslut, new Access_token().getClass()); if (access_token.getAccess_token() != null ) { System.out.println( "獲取的access_token是:" + access_token.getAccess_token()); System.out.println( "該access_token的有效時(shí)間是:" + access_token.getExpires_in() + "s" ); } else { System.out.println(TAG + "獲取access_token失敗,請(qǐng)檢查" ); } } |
保存接口調(diào)用憑證access_token
•思路
將獲取到的Access_token和當(dāng)前時(shí)間存儲(chǔ)到file里, 取出時(shí)判斷當(dāng)前時(shí)間和存儲(chǔ)里面的記錄的時(shí)間的時(shí)間差,如果大于MAX_TIME,重新獲取,并且將獲取到的存儲(chǔ)到file替換原來的內(nèi)容,如果小于MAX_TIME,直接獲取。
•實(shí)現(xiàn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/* * 該方法實(shí)現(xiàn)獲取Access_token、保存并且只保存2小時(shí)Access_token。如果超過兩個(gè)小時(shí)重新獲取;如果沒有超過兩個(gè)小時(shí),直接獲取。該方法依賴 * :public static String getAccessToken(); * * 思路:將獲取到的Access_token和當(dāng)前時(shí)間存儲(chǔ)到file里, * 取出時(shí)判斷當(dāng)前時(shí)間和存儲(chǔ)里面的記錄的時(shí)間的時(shí)間差,如果大于MAX_TIME,重新獲取,并且將獲取到的存儲(chǔ)到file替換原來的內(nèi)容 * ,如果小于MAX_TIME,直接獲取。 */ @Test public void getSavedAccess_token() throws IOException { Gson gson = new Gson(); String mAccess_token = null;// 需要獲取的Access_token; File file = new File("temp_access_token.temp");// Access_token保存的位置 // 如果文件不存在,創(chuàng)建 if (!file.exists()) file.createNewFile(); // 如果文件大小等于0,說明第一次使用,存入Access_token if (file.length() == 0) { mAccess_token = getAccessToken(); FileOutputStream fos = new FileOutputStream(file, false);// 不允許追加 Access_token at = new Access_token(); at.setAccess_token(mAccess_token); at.setExpires_in(System.currentTimeMillis() + ""); String json = gson.toJson(at); fos.write((json).getBytes()); fos.close(); } else { // 讀取文件內(nèi)容 FileInputStream fis = new FileInputStream(file); byte[] b = new byte[2048]; int len = fis.read(b); String mJsonAccess_token = new String(b, 0, len);// 讀取到的文件內(nèi)容 Access_token access_token = gson.fromJson(mJsonAccess_token, new Access_token().getClass()); if (access_token.getExpires_in() != null) { long saveTime = Long.parseLong(access_token.getExpires_in()); long nowTime = System.currentTimeMillis(); long remianTime = nowTime - saveTime; // System.out.println(TAG + "時(shí)間差:" + remianTime); if (remianTime < MAX_TIME) { Access_token at = gson.fromJson(mJsonAccess_token, new Access_token().getClass()); mAccess_token = at.getAccess_token(); } else { mAccess_token = getAccessToken(); FileOutputStream fos = new FileOutputStream(file, false);// 不允許追加 Access_token at = new Access_token(); at.setAccess_token(mAccess_token); at.setExpires_in(System.currentTimeMillis() + ""); String json = gson.toJson(at); fos.write((json).getBytes()); fos.close(); } } } System.out.println("獲取到的Access_token是:" + mAccess_token); } /* * 獲取微信服務(wù)器AccessToken。該部分和getAccess_token() 一致,不再加注釋 */ public static String getAccessToken() { String urlString = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECERT; String reslut = null ; try { URL reqURL = new URL(urlString); HttpsURLConnection httpsConn = (HttpsURLConnection) reqURL .openConnection(); InputStreamReader isr = new InputStreamReader( httpsConn.getInputStream()); char [] chars = new char [ 1024 ]; reslut = "" ; int len; while ((len = isr.read(chars)) != - 1 ) { reslut += new String(chars, 0 , len); } isr.close(); } catch (IOException e) { e.printStackTrace(); } Gson gson = new Gson(); Access_token access_token = gson.fromJson(reslut, new Access_token().getClass()); if (access_token.getAccess_token() != null ) { return access_token.getAccess_token(); } else { return null ; } } |
前三篇文章演示源碼:WeixinApiDemo.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/wgyscsf/article/details/51078372