更新一波,微信第三方開發平台授權流程


最近一直忙於微信三方開發平台開發,更新一下,做個記錄,微信第三方開發平台授權流程示例:

先看授權流程要拿到的結果:

微信第三方授權

照例先給出微信授權流程官網頁面:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=zh_CN

接下來是授權流程:

文檔:

說明

授權的方式分為兩種,分別為:

 

說明

此過程中我這邊使用的是第一種方法。以上屬於文檔中給定的總的授權方法,接下來我們去實現它:

首先看第一步:獲取第三方平台componment_access_token:

官方文檔:

獲取componmentAccessToken

實現:

Postman運行示例:

postman

代碼示例:

 1 /**
 2      * 調取第三方平台component_access_token(有效期兩個小時)的接口
 3      */
 4     public static Pubcache getNewComponentAccessToken(){
 5         Map<String, String> map = new HashMap<>();
 6         map.put("component_appid", WXconfig.COMPONENT_APPID);
 7         map.put("component_appsecret", WXconfig.COMPONENT_APPSECRET);
 8         map.put("component_verify_ticket", WxCacheUtil.getComponentVerifyTicket());
 9         DebugUtils.savaDebugInfo(null, "從緩存中取出的為component_verify_ticket====================", WxCacheUtil.getComponentVerifyTicket());
10         String postData = JsonUtils.convert(map);
11         String url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
12         String result = WxUtil.httpsRequest(url, "POST", postData);
13         Map<String, String> cat = CompenmentAuthorize.parseJSONMap(result);
14         //存緩存
15         Pubcache pubcache = new Pubcache();
16         pubcache.setKeyName("component_access_token");
17         pubcache.setKeyValue(cat.get("component_access_token"));
18         pubcache.setLastUpdate(new Date());
19         return pubcache;
20     }

說明:componmentVerifyTicket是微信第三方全網發布之后每隔十分鍾推送的ticket,我在接收ticket的時候存入了數據庫中,所以此時的WxCacheUtil.getComponentVerifyTicket()方法是從數據庫中讀取的最新推送的一條數據;WXconfig.COMPONENT_APPID是三方商務平台的appid,

WXconfig.COMPONENT_APPSECRET是三方商務平台的appsecret;

運行結果返回和微信文檔中返回一致則正確,此時成功獲取到最新的componment_access_token;因為componment_access_token有效期兩個小時,建議緩存起來

或者存入數據庫,在考慮並發的情況下建議寫個定時器在一分五十秒左右去定時刷新componment_access_token,避免componment_access_token失效或者被別處

方法調用而更新掉導致無效。

第二步:獲取預授權碼pre_auth_code:

官方文檔:

獲取預授權碼

postman運行示例:

postman示例

postman示例

參數說明:param里面的componment_access_token就是上一步驟中獲取到的componment_access_token,然后post請求中的componment_appid就是三方商務平台

的appid,然后運行postman,結果會返回預授權碼以及有效時間。

第三步:使用授權碼換取公眾號授權信息:

這個時候使用一個寫好的頁面:

 1 <html>
 2 <head>
 3 <meta charset="utf-8">
 4 
 5 <meta name="keywords" content="">
 6 <meta name="description" content="">
 7 <title></title>
 8 <script src="STYLE/JS/jquery-1.8.3.min.js" type="text/javascript" charset="utf-8"></script>
 9 <script type="text/javascript"> 
10     //獲取鏈接參數函數
11     function goToAuth(){
12         var url = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=12345678&pre_auth_code=xxxxxxx&redirect_uri=http%3A%2F%2Fxxx.com%2Fxxxx%2Fevent%2xxxxx.do&auth_type=1";
13         window.location.href = url;
14     };
15 </script> 
16 </head>
17 <body>
18     <div class="main"> 
19         <div class="ewm"> 
20             <input type="button" value="授權" onclick="goToAuth();">   
21         </div>
22     </div>
23 </body>
24 </html>

說明:把頁面js中的componment_appid換成自己這邊三方商務平台的componment_appid,把pre_auth_code替換成剛才獲取到的預授權碼,然后再把回跳地址替換一下,運行此頁面會生成一個授權二維碼頁面,當公眾號管理員掃描進行授權之后,回調url會返回授權碼authorization_code和過期時間。

第四步:獲取授權信息:

微信文檔:

獲取授權信息

postMan示例:

postman示例

 

postman示例

填入對應的參數,運行postman,獲取到授權信息保存即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM