一、什么是access_token?
access_token是公眾號的全局唯一票據,公眾號調用各接口時都需使用access_token。正常情況下access_token有效期為7200秒,重復獲取將導致上次獲取的access_token失效。由于獲取access_token的api調用次數非常有限,建議開發者全局存儲與更新access_token,頻繁刷新access_token會導致api調用受限,影響自身業務。
二、要解決的問題
1、如何獲取access_token。
2、由于access_token的有效期為7200秒,即2小時,并且重復獲取將導致上次獲取的access_token失效,獲取access_token的api調用次數非常有限,所以要解決如何全局存儲與更新access_token。
三、思路
1、將access_token存儲在數據庫中。
2、何時更新access_token呢?當access_token失效的時候更新,那么怎么判斷access_token有沒有失效呢?使用當前的access_token請求微信接口,獲取自定義菜單,如果返回的errcode為42001,則說明access_token已經失效,這時再重新獲取access_token。
數據庫設計(表名swx_config):
四、代碼:
1、http請求代碼(httprequestutil類):
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 請求url,不發送數據 /// <summary> /// 請求url,不發送數據 /// </summary> public static string requesturl( string url) { return requesturl(url, "post" ); } #endregion #region 請求url,不發送數據 /// <summary> /// 請求url,不發送數據 /// </summary> public static string requesturl( string url, string method) { // 設置參數 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" ); //發送請求并獲取相應回應數據 httpwebresponse response = request.getresponse() as httpwebresponse; //直到request.getresponse()程序才開始向目標網頁發送post請求 stream responsestream = response.getresponsestream(); streamreader sr = new streamreader(responsestream, encoding.utf8); //返回結果網頁(html)代碼 string content = sr.readtoend(); return content; } #endregion |
2、輔助方法(tools類):
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> /// 工具類 /// </summary> public class tools { #region 獲取json字符串某節點的值 /// <summary> /// 獲取json字符串某節點的值 /// </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) { //先截逗號,若是最后一個,截“}”號,取最小值 int end = jsonstr.indexof( ',' , index); if (end == -1) { end = jsonstr.indexof( '}' , index); } result = jsonstr.substring(index, end - index); result = result.trim( new char [] { '"' , ' ' , '\'' }); //過濾引號或空格 } } return result; } #endregion } } |
3、判斷access_token是否過期(wxapi類):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#region 驗證token是否過期 /// <summary> /// 驗證token是否過期 /// </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、請求微信接口,獲取access_token(wxapi類):
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、全局存儲與更新access_token(adminutil類):
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)) //尚未保存過access_token { access_token = wxapi.gettoken(user.appid, user.appsecret); } else { if (wxapi.tokenexpired(user.access_token)) //access_token過期 { 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 |
以上就是本文的全部內容,希望對大家進行微信公眾平臺開發有所幫助。