一、什么是access_token?
access_token是公眾號(hào)的全局唯一票據(jù),公眾號(hào)調(diào)用各接口時(shí)都需使用access_token。正常情況下access_token有效期為7200秒,重復(fù)獲取將導(dǎo)致上次獲取的access_token失效。由于獲取access_token的api調(diào)用次數(shù)非常有限,建議開(kāi)發(fā)者全局存儲(chǔ)與更新access_token,頻繁刷新access_token會(huì)導(dǎo)致api調(diào)用受限,影響自身業(yè)務(wù)。
二、要解決的問(wèn)題
1、如何獲取access_token。
2、由于access_token的有效期為7200秒,即2小時(shí),并且重復(fù)獲取將導(dǎo)致上次獲取的access_token失效,獲取access_token的api調(diào)用次數(shù)非常有限,所以要解決如何全局存儲(chǔ)與更新access_token。
三、思路
1、將access_token存儲(chǔ)在數(shù)據(jù)庫(kù)中。
2、何時(shí)更新access_token呢?當(dāng)access_token失效的時(shí)候更新,那么怎么判斷access_token有沒(méi)有失效呢?使用當(dāng)前的access_token請(qǐng)求微信接口,獲取自定義菜單,如果返回的errcode為42001,則說(shuō)明access_token已經(jīng)失效,這時(shí)再重新獲取access_token。
數(shù)據(jù)庫(kù)設(shè)計(jì)(表名swx_config):
四、代碼:
1、http請(qǐng)求代碼(httprequestutil類(lèi)):
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
|
#region 請(qǐng)求url,不發(fā)送數(shù)據(jù) /// <summary> /// 請(qǐng)求url,不發(fā)送數(shù)據(jù) /// </summary> public static string requesturl( string url) { return requesturl(url, "post" ); } #endregion #region 請(qǐng)求url,不發(fā)送數(shù)據(jù) /// <summary> /// 請(qǐng)求url,不發(fā)送數(shù)據(jù) /// </summary> public static string requesturl( string url, string method) { // 設(shè)置參數(shù) httpwebrequest request = webrequest.create(url) as httpwebrequest; cookiecontainer cookiecontainer = new cookiecontainer(); request.cookiecontainer = cookiecontainer; request.allowautoredirect = true ; request.method = method; request.contenttype = "text/html" ; request.headers.add( "charset" , "utf-8" ); //發(fā)送請(qǐng)求并獲取相應(yīng)回應(yīng)數(shù)據(jù) httpwebresponse response = request.getresponse() as httpwebresponse; //直到request.getresponse()程序才開(kāi)始向目標(biāo)網(wǎng)頁(yè)發(fā)送post請(qǐng)求 stream responsestream = response.getresponsestream(); streamreader sr = new streamreader(responsestream, encoding.utf8); //返回結(jié)果網(wǎng)頁(yè)(html)代碼 string content = sr.readtoend(); return content; } #endregion |
2、輔助方法(tools類(lèi)):
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
|
namespace swx.utils { /// <summary> /// 工具類(lèi) /// </summary> public class tools { #region 獲取json字符串某節(jié)點(diǎn)的值 /// <summary> /// 獲取json字符串某節(jié)點(diǎn)的值 /// </summary> public static string getjsonvalue( string jsonstr, string key) { string result = string .empty; if (! string .isnullorempty(jsonstr)) { key = "\"" + key.trim( '"' ) + "\ "" ; int index = jsonstr.indexof(key) + key.length + 1; if (index > key.length + 1) { //先截逗號(hào),若是最后一個(gè),截“}”號(hào),取最小值 int end = jsonstr.indexof( ',' , index); if (end == -1) { end = jsonstr.indexof( '}' , index); } result = jsonstr.substring(index, end - index); result = result.trim( new char [] { '"' , ' ' , '\'' }); //過(guò)濾引號(hào)或空格 } } return result; } #endregion } } |
3、判斷access_token是否過(guò)期(wxapi類(lèi)):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#region 驗(yàn)證token是否過(guò)期 /// <summary> /// 驗(yàn)證token是否過(guò)期 /// </summary> public static bool tokenexpired( string access_token) { string jsonstr = httprequestutil.requesturl( string .format( "https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}" , access_token)); if (tools.getjsonvalue(jsonstr, "errcode" ) == "42001" ) { return true ; } return false ; } #endregion |
4、請(qǐng)求微信接口,獲取access_token(wxapi類(lèi)):
1
2
3
4
5
6
7
8
9
10
|
#region 獲取token /// <summary> /// 獲取token /// </summary> public static string gettoken( string appid, string secret) { string strjson = httprequestutil.requesturl( string .format( "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}" , appid, secret)); return tools.getjsonvalue(strjson, "access_token" ); } #endregion |
5、全局存儲(chǔ)與更新access_token(adminutil類(lèi)):
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
|
#region 獲取access_token /// <summary> /// 獲取access_token /// </summary> public static string getaccesstoken(pagebase page) { string access_token = string .empty; userinfo user = getloginuser(page); if (user != null ) { if ( string .isnullorwhitespace(user.access_token)) //尚未保存過(guò)access_token { access_token = wxapi.gettoken(user.appid, user.appsecret); } else { if (wxapi.tokenexpired(user.access_token)) //access_token過(guò)期 { access_token = wxapi.gettoken(user.appid, user.appsecret); } else { return user.access_token; } } mssqlhelper.executesql( string .format( "update swx_config set access_token='{0}' where username='{1}'" , access_token, user.username)); } return access_token; } #endregion |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家進(jìn)行微信公眾平臺(tái)開(kāi)發(fā)有所幫助。