标签:
最近公司涉及到微信绑定用户,做了高级接口-申请带参数的二维码,总结了下微信开发接口。微信接口开发都是除了消息用的xml 回复基本上都是用json的形式传递信息(post/get),开发的方法基本都是一样的,以下以高级接口获取带参数的二维码为例,怎么做微信接口开发。
第一步:获取access_token,access_token是获取其他接口信息的钥匙,所有接口都需要调用access_token
/// <summary> /// 拉取AccessToken,微信每天公共2000次AccessToken的获取,所以需要缓存AccessToken /// </summary> /// <returns>用户凭证:AccessToken</returns> public string getTokenSession(string appid,string appsecret) { string TokenSession; if (Session[AccessToken] != null) { TokenSession = Session[AccessToken].ToString(); } else { TokenSession = addTokenSession(appid, appsecret); } return TokenSession; } /// <summary> /// 将AccessToken添加到缓存 /// </summary> /// <returns>AccessToken</returns> public string addTokenSession(string appid, string appsecret) { //获取AccessToken string AccessToken = GetAccessToken(string appid, string appsecret); HttpContext.Current.Session[AccessToken] = AccessToken; HttpContext.Current.Session.Timeout = 7200; return AccessToken; } /// <summary> /// 获取AccessToken /// </summary> /// <returns>AccessToken</returns> public string GetAccessToken(string appid, string appsecret) { string accessToken = ""; string respText = ""; string url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, appsecret); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream resStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(resStream, Encoding.Default); respText = reader.ReadToEnd(); resStream.Close(); } Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText); accessToken = respDic["access_token"].ToString(); return accessToken; }
第二步:利用access_token来获取带参数的二维码
/// <summary> /// 调用微信接口获取带参数临时二维码的ticket /// 使用方法:https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET /// </summary> /// <param name="scene_id">二维码带的参数</param> /// <returns>json:ticket:换取二维码的凭证,expire_seconds:凭证有效时间,url:二维码解析后的地址。此处返回ticket 否则返回错误码</returns> public string GetQrcode(string appid, string appsecret, Int32 scene_id) { string QrcodeUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}";//WxQrcodeAPI接口 string AccessToken = getTokenSession(appid, appsecret);//拉取AccessToken QrcodeUrl = string.Format(QrcodeUrl, AccessToken); string PostJson = "{\"expire_seconds\": 1800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": " + scene_id + "}}}"; string ReText = WebRequestPostOrGet(QrcodeUrl, PostJson);//post提交 Dictionary<string, object> reDic = (Dictionary<string, object>)Jss.DeserializeObject(ReText); if (reDic.ContainsKey("ticket")) { return reDic["ticket"].ToString();//成功 } else { return reDic["errcode"].ToString();//返回错误码 } } /// <summary> /// 调用微信接口获取带参数永久二维码的ticket /// 使用方法:https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET /// </summary> /// <param name="scene_id">二维码带的参数</param> /// <returns>json:ticket:换取二维码的凭证,expire_seconds:凭证有效时间,url:二维码解析后的地址。此处返回ticket 否则返回错误码</returns> public string GetQrcode(string appid, string appsecret, string scene_str) { string QrcodeUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}";//WxQrcodeAPI接口 string AccessToken = getTokenSession(appid, appsecret);//拉取AccessToken QrcodeUrl = string.Format(QrcodeUrl, AccessToken); string PostJson = "{\"expire_seconds\": 1800, \"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": " + scene_str + "}}}"; string ReText = WebRequestPostOrGet(QrcodeUrl, PostJson);//此处省略了 WebRequestPostOrGet即为WebHttpRequest发送Post请求 Dictionary<string, object> reDic = (Dictionary<string, object>)Jss.DeserializeObject(ReText); if (reDic.ContainsKey("ticket")) { return reDic["ticket"].ToString();//成功 } else { return reDic["errcode"].ToString();//返回错误码 } }
第三步:将获取的ticket用https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET使用就完成了。
微信开发者文档:http://mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html
标签:
原文地址:http://www.cnblogs.com/net-xiejun/p/4422890.html