公衆號第三方平臺開發 獲取 component_verify_ticket 2015-07-05 10:16 59人閱讀 評論(0) 收藏

八、推送component_verify_ticket協議

在公衆號第三方平臺建立審覈經過後,微信服務器會向其「受權事件接收URL」每隔10分鐘定時推送component_verify_ticket。第三方平臺方在收到ticket推送後也需進行解密(詳細請見【消息加解密接入指引】),接收到後必須直接返回字符串success。html

上面是微信API裏的原話,在回調頁面作了下日誌記錄:服務器

<span style="font-family:SimSun;font-size:12px;">            string post = "";
            string get = "";
            for (int i = 0; i < Request.Form.Count; i++)
            {
                post += Request.Form.Keys[i] + "=" + Request.Form[i] + " ";
            }
            for (int i = 0; i < Request.QueryString.Count; i++)
            {
                get += Request.QueryString.Keys[i] + "=" + Request.QueryString[i] + " ";
            }</span>
可是,日誌記錄的Post和Get參數,都沒內容 component_verify_ticket 相關的內容。

後面通過一番查詢,原來是要這樣才能獲取到微信推送的內容:微信

            byte[] data = Request.BinaryRead(Request.TotalBytes);
           string postData = Encoding.Default.GetString(data);


            //公衆號第三方平臺的appid
            string appId = ConfigurationManager.AppSettings["WeixinAppID"];
            //第三方平臺申請時填寫的接收消息的校驗token
            string token = ConfigurationManager.AppSettings["WeixinToken"];
            //第三方平臺申請時填寫的接收消息的加密symmetric_key
            string encodingAesKey = ConfigurationManager.AppSettings["WeixinEncodingAESKey"];
            string sMsg = "";//解密後的內容
            var msg = new WXBizMsgCrypt(token, encodingAesKey, appId);
            int ret = msg.DecryptMsg(
                Request.QueryString[" msg_signature "],
                Request.QueryString["timestamp"],
                Request.QueryString["nonce"],
                postData,
                ref sMsg);
            AppService.LogService.Log("回調參數", "構造參數:" +
                                              " appId:" + appId +
                                              " token:" + token +
                                              " encodingAesKey:" + encodingAesKey + " 解密參數:" +
                                              " signature:" + Request.QueryString["signature"] +
                                              " msg_signature:" + Request.QueryString["msg_signature"] +
                                              " timestamp:" + Request.QueryString["timestamp"] +
                                              " nonce:" + Request.QueryString["nonce"] +
                                              " postData:" + postData
                );

            AppService.LogService.Log("解密結果", ret + " " + sMsg);app

解密後獲得的結果大概是這樣:post

<xml>
<AppId>
    <![CDATA[wx34b5f92cdbb24fe4]]>
</AppId>
<CreateTime>1436066428</CreateTime>
<InfoType>
    <![CDATA[component_verify_ticket]]>
</InfoType>
<ComponentVerifyTicket>
    <![CDATA[ticket@@@SfS2Ck4oxPhL0LRSJH5v9Pr68vjeDfWWPx9SzIWzm6PeKGeEoszohQ-5-EBm4RajI7AKlzIz1Kj1ghPg0t199w]]>
</ComponentVerifyTicket>
</xml>

ComponentVerifyTicket節點便是咱們須要的結果。
加密

但願遇到一樣問題的童鞋不用走彎路。spa

版權聲明:本文爲博主原創文章,未經博主容許不得轉載。日誌

相關文章
相關標籤/搜索