//此方法已经过期,新的媒体文件上传方法请见 AdvancedAPIs/Media ///// <summary> ///// 媒体文件上传接口 /////注意事项 /////1.上传的媒体文件限制: /////图片(image) : 1MB,支持JPG格式 /////语音(voice):1MB,播放长度不超过60s,支持MP4格式 /////视频(video):10MB,支持MP4格式 /////缩略图(thumb):64KB,支持JPG格式 /////2.媒体文件在后台保存时间为3天,即3天后media_id失效 ///// </summary> ///// <param name="accessToken"></param> ///// <param name="type">上传文件类型</param> ///// <param name="fileName">上传文件完整路径+文件名</param> ///// <returns></returns> //public static UploadMediaFileResult UploadMediaFile(string accessToken, UploadMediaFileType type, string fileName) //{ // var cookieContainer = new CookieContainer(); // var fileStream = FileHelper.GetFileStream(fileName); // var url = string.Format("http://api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}&filename={2}&filelength={3}", // accessToken, type.ToString(), Path.GetFileName(fileName), fileStream != null ? fileStream.Length : 0); // UploadMediaFileResult result = Post.PostGetJson<UploadMediaFileResult>(url, cookieContainer, fileStream); // return result; //} /// <summary> /// 获取调用微信JS接口的临时票据 /// </summary> /// <param name="appId"></param> /// <param name="secret"></param> /// <param name="type"></param> /// <returns></returns> public static JsApiTicketResult GetTicket(string appId, string secret, string type = "jsapi") { var accessToken = AccessTokenContainer.TryGetToken(appId, secret); var url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type={1}", accessToken, type); JsApiTicketResult result = Get.GetJson <JsApiTicketResult>(url); return(result); }
/// <summary> /// 使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="appId"></param> /// <param name="appSecret"></param> /// <param name="fun">第一个参数为accessToken</param> /// <param name="retryIfFaild"></param> /// <returns></returns> public static T Do <T>(string appId, string appSecret, Func <string, T> fun, bool retryIfFaild = true) where T : WxJsonResult { T result = null; try { var accessToken = AccessTokenContainer.TryGetToken(appId, appSecret, false); result = fun(accessToken); } catch (ErrorJsonResultException ex) { if (retryIfFaild && ex.JsonResult.errcode == ReturnCode.获取access_token时AppSecret错误或者access_token无效) { //尝试重新验证 var accessToken = AccessTokenContainer.TryGetToken(appId, appSecret, true); result = Do <T>(appId, appSecret, fun, false); } } return(result); }