公众号第三方平台开发 component_verify_ticket和accessToken的获取
生活随笔
收集整理的這篇文章主要介紹了
公众号第三方平台开发 component_verify_ticket和accessToken的获取
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
公眾號第三方平臺審核通過之后,微信的服務(wù)器會定時(10分鐘一次)給"授權(quán)事件接收URL"發(fā)送component_verify_ticket,這里我們需要及時更新component_verify_ticket
就是下面這個鏈接
下面這里是我的處理函數(shù)
private void ResponseRequest()
{
//WXBizMsgCrypt 這個類是騰訊提供的,下載地址是http://mp.weixin.qq.com/wiki/static/assets/a5a22f38cb60228cb32ab61d9e4c414b.zip
//這里的構(gòu)造函數(shù)我自己改寫了,騰訊提供的構(gòu)造函數(shù)需要提供三個參數(shù)的,具體請看微信提供的示例代碼
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} 解析后的內(nèi)容:{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,通知微信服務(wù)器已經(jīng)接收到ticket
Response.Write("success");
Response.End();
break;
case "unauthorized":
//當(dāng)用戶取消授權(quán)的時候,微信服務(wù)器也會向這個頁面發(fā)送信息,在這里做一下記錄
LogTool.LogToDirDest(filePath, LogType.Debug, string.Format("{1} {0} 已取消授權(quán)", q.Elements("AuthorizerAppid").First().Value, pageUrl));
Response.End();
break;
default:
break;
}
}
}
補充一下我的GetPost()函數(shù)
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作為參數(shù),向微信服務(wù)器發(fā)送請求即可接收到第三方平臺的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結(jié)果示例如下:
{
"component_access_token":"61W3mEpU66027wgNZ_MhGHNQDHnFATkDa9-2llqrMBjUwxRSNPbVsMmyD-yq8wZETSoE5NQgecigDrSHkPtIYA",
"expires_in":7200
}
自己轉(zhuǎn)換一下即可
總結(jié)
以上是生活随笔為你收集整理的公众号第三方平台开发 component_verify_ticket和accessToken的获取的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最长公共子序列LCS
- 下一篇: acm公选课第三节4.7直播4.9补 递