公眾號第三方平台開發 教程一 創建公眾號第三方平台
公眾號第三方平台開發 教程二 component_verify_ticket和accessToken的獲取
公眾號第三方平台開發 教程三 微信公眾號授權第三方平台
公眾號第三方平台開發 教程四 代公眾號發起網頁授權說明
公眾號第三方平台開發 教程五 代公眾號處理消息和事件
公眾號第三方平台開發 教程六 代公眾號使用JS SDK說明
另,感謝一下這個大蝦的博客,這幾篇東西都是在他的博文基礎上完成的,他的博客里也有一些接口代碼可以下載
微信開發系列教程
就是下面這個鏈接
下面這里是我的處理函數
private void ResponseRequest() { //WXBizMsgCrypt 這個類是騰訊提供的,下載地址是http://mp.weixin.qq.com/wiki/static/assets/a5a22f38cb60228cb32ab61d9e4c414b.zip //這里的構造函數我自己改寫了,騰訊提供的構造函數需要提供三個參數的,具體請看微信提供的示例代碼 WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(); string sReqMsgSig = Request["msg_signature"]; string sReqTimeStamp = Request["timestamp"]; string sReqNonce = Request["nonce"]; string sReqData = GetPost(); LogTool.LogToDirDest(filePath, LogType.Debug, string.Format("{1} 接收到的信息:{0}", sReqData, pageUrl)); string sMsg = ""; //解析之后的明文 int ret = 0; ret = wxcpt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, sReqData, ref sMsg); if (ret != 0) { LogTool.LogToDirDest(filePath, LogType.Error, string.Format("{1} 解析出錯:{0}", ret, pageUrl)); } else { LogTool.LogToDirDest(filePath, LogType.Debug, string.Format("{1} 解析后的內容:{0}", sMsg, pageUrl)); var xDoc = XDocument.Parse(sMsg); List<XElement> q = (from c in xDoc.Elements() select c).ToList(); var infoType = q.Elements("InfoType").First().Value; switch (infoType) { case "component_verify_ticket": //q.Elements("ComponentVerifyTicket").First() //這里就是component_verify_ticket的值,保存起來就可以了,處理完成后在頁面上輸出success,通知微信服務器已經接收到ticket Response.Write("success"); Response.End(); break; case "unauthorized": //當用戶取消授權的時候,微信服務器也會向這個頁面發送信息,在這里做一下記錄 LogTool.LogToDirDest(filePath, LogType.Debug, string.Format("{1} {0} 已取消授權", q.Elements("AuthorizerAppid").First().Value, pageUrl)); Response.End(); break; default: break; } } }
補充一下我的GetPost()函數
public string GetPost() { try { System.IO.Stream s = Request.InputStream; int count = 0; byte[] buffer = new byte[s.Length]; StringBuilder builder = new StringBuilder(); while ((count = s.Read(buffer, 0, buffer.Length)) > 0) { builder.Append(Request.ContentEncoding.GetString(buffer, 0, count)); } s.Flush(); s.Close(); s.Dispose(); return builder.ToString(); } catch (Exception ex) { throw ex; } }
利用上面獲得的component_verify_ticket作為參數,向微信服務器發送請求即可接收到第三方平台的accessToken
/// <summary> /// 獲取第三方平台access_token /// </summary> /// <param name="component_appid"></param> /// <param name="component_appsecret"></param> /// <param name="component_verify_ticket"></param> /// <returns></returns> public static ResponseComponentToken Component_token(string component_verify_ticket) { var urlFormat = "https://api.weixin.qq.com/cgi-bin/component/api_component_token"; object data = null; data = new { component_appid = Config.ServerAppID, component_appsecret = Config.ServerAppSecret, component_verify_ticket = component_verify_ticket }; return CommonJsonSend.Send<ResponseComponentToken>("", urlFormat, data, timeOut: Config.TIME_OUT); }
如果請求成功,返回的json結果示例如下:
{
"component_access_token":"61W3mEpU66027wgNZ_MhGHNQDHnFATkDa9-2llqrMBjUwxRSNPbVsMmyD-yq8wZETSoE5NQgecigDrSHkPtIYA",
"expires_in":7200
}
自己轉換一下即可