access_token是公眾號的全局唯一票據,公眾號調用各接口時都需使用access_token。開發者需要進行妥善保存。access_token的存儲至少要保留512個字符空間。access_token的有效期目前為2個小時,需定時刷新,重復獲取將導致上次獲取的access_token失效。
公眾平臺的API調用所需的access_token的使用及生成方式說明:
1、為了保密appsecrect,第三方需要一個access_token獲取和刷新的中控服務器。而其他業務邏輯服務器所使用的access_token均來自于該中控服務器,不應該各自去刷新,否則會造成access_token覆蓋而影響業務;
2、目前access_token的有效期通過返回的expire_in來傳達,目前是7200秒之內的值。中控服務器需要根據這個有效時間提前去刷新新access_token。在刷新過程中,中控服務器對外輸出的依然是老access_token,此時公眾平臺后臺會保證在刷新短時間內,新老access_token都可用,這保證了第三方業務的平滑過渡;
3、access_token的有效時間可能會在未來有調整,所以中控服務器不僅需要內部定時主動刷新,還需要提供被動刷新access_token的接口,這樣便于業務服務器在API調用獲知access_token已超時的情況下,可以觸發access_token的刷新流程。
如果第三方不使用中控服務器,而是選擇各個業務邏輯點各自去刷新access_token,那么就可能會產生沖突,導致服務不穩定。
公眾號可以使用AppID和AppSecret調用本接口來獲取access_token。AppID和AppSecret可在微信公眾平臺官網-開發者中心頁中獲得(需要已經成為開發者,且帳號沒有異常狀態)。注意調用所有微信接口時均需使用https協議。
接口調用請求說明
http請求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
參數說明
返回說明
正常情況下,微信會返回下述JSON數據包給公眾號:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
錯誤時微信會返回錯誤碼等信息,JSON數據包示例如下(該示例為AppID無效錯誤):
{"errcode":40013,"errmsg":"invalid appid"}
2.代碼實現
APPID,APPSECRET在公眾賬號中可查詢
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
94
95
96
97
98
99
100
101
102
103
104
105
106
|
package com.zhrd.bussinss.platform.scheduled; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import com.zhrd.bussinss.platform.constants.WeiXinId; import com.zhrd.bussinss.platform.service.AccessTokenService; import net.sf.json.JSONObject; @Component @Lazy ( false ) public class GetWeiXinAccessTokenScheduled { /** * 獲得ACCESS_TOKEN * * @Title: getAccess_token * @Description: 獲得ACCESS_TOKEN * @param @return 設定文件 * @return String 返回類型 * @throws */ @Autowired private AccessTokenService accessTokenServiceImpl; @Scheduled (fixedRateString = "${weixin.token.fixedRate.in.milliseconds}" , initialDelayString = "${weixin.token.initialDelay.in.milliseconds}" ) public void getAccessToken() { System.out.println( "====================獲取token開始==============================" ); String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + WeiXinId.APPID+ "&secret=" + WeiXinId.APPSECRET; String accessToken = null ; String expiresIn = null ; try { URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); http.setRequestMethod( "GET" ); // 必須是get方式請求 http.setRequestProperty( "Content-Type" , "application/x-www-form-urlencoded" ); http.setDoOutput( true ); http.setDoInput( true ); http.connect(); InputStream is = http.getInputStream(); int size = is.available(); byte [] jsonBytes = new byte [size]; is.read(jsonBytes); String message = new String(jsonBytes, "UTF-8" ); JSONObject demoJson = JSONObject.fromObject(message); accessToken = demoJson.getString( "access_token" ); expiresIn = demoJson.getString( "expires_in" ); System.out.println( "accessToken====" +accessToken); System.out.println( "expiresIn===" +expiresIn); accessTokenServiceImpl.addToken(accessToken,expiresIn); System.out.println( "====================獲取token結束==============================" ); is.close(); } catch (Exception e) { e.printStackTrace(); } // return accessToken; } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。