微信公衆平臺,做爲自媒體的旗艦級產品,愈來愈多的人已經投入它的懷抱。正如它的廣告詞所說:再小的個體,也有品牌html
好吧,閒話很少說,今天要說的是它的IP白名單機制。api
咱們如今安裝的大部分都是電信的家庭寬帶,它的公網IP是隨機的,固然也能夠加錢變成固定IP,也能夠經過其餘軟件映射,均可以變成固定IP微信
我說的這個方案,也算其中一種吧,就是經過WebAPI方式,把某一臺機器的公網IP固定,而後訪問公衆號的程序部署在這上面,最後這些程序再經過WebAPI方式被其餘客戶端調用微信公衆平臺
好了,仍是上代碼吧框架
//3,需先將封面上傳至目標公衆號 //3.1,下載原公衆號中封面圖片.判斷是否存在?不存在,則下載 //注:需判斷文件夾是否存在?若不存在,則建立 string folderName = CurrentAppInfo.AppPath + @"\MaterialFile\thumb\"; if (!Directory.Exists(folderName)) { Directory.CreateDirectory(folderName); } string fileFullName = folderName + sourceThumbMediaId + ".png"; FileInfo fi = new FileInfo(fileFullName); if (!fi.Exists) { string sourceAccessToken = this.wxpService.GetAccessToken(this.sourceSourceID); Stream imageStream = this.wxpService.GetMaterial(sourceAccessToken, sourceThumbMediaId); Image image = Image.FromStream(imageStream); Bitmap bitmap = new Bitmap(image); bitmap.Save(fileFullName); } //3.2,再上傳至目標公衆號 //此處是上傳封面圖片 string targetAccessToken = this.wxpService.GetAccessToken(this.targetSourceID); UploadMaterialApiResultModel modelApiResult1 = this.wxpService.UploadMaterialAny(targetAccessToken, new wxpmaterialBillModel() { IsTemp = "0", FileFullName = fi.FullName, MaterialType = MaterialType.thumb.ToString() }); string targetThumbMediaId = modelApiResult1.media_id; //3,加工數據 wxpapiaccountBillModel modelSourceApiAccount = this.listApiAccount.FirstOrDefault(c => c.SourceID == this.sourceSourceID); wxpapiaccountBillModel modelTargetApiAccount = this.listApiAccount.FirstOrDefault(c => c.SourceID == this.targetSourceID); content = content.Replace(modelSourceApiAccount.SourceCode, modelTargetApiAccount.SourceCode); content = content.Replace(modelSourceApiAccount.SourceName, modelTargetApiAccount.SourceName); //二維碼圖片可能有多個,分隔符是:分號 //注:目標地址只獲取第1個便可 string[] qrcodeUrl1s = modelSourceApiAccount.QRCodeUrlPath.Split(';'); string qrcodeUrl2 = modelTargetApiAccount.QRCodeUrlPath.Split(';')[0]; foreach (string qrcodeUrl1 in qrcodeUrl1s) { content = content.Replace(qrcodeUrl1, qrcodeUrl2); } UploadNewsModel modelUpload = new UploadNewsModel() { title = title, author = author, digest = digest, content = content, content_source_url = targetSourceUrl, show_cover_pic = "0", thumb_media_id = targetThumbMediaId }; //4,上傳素材至目標公衆號 UploadMaterialApiResultModel modelResult = this.wxpService.UploadMaterialNews(targetAccessToken, new List<UploadNewsModel>() { modelUpload }); //4.1,需標記源公衆號中此素材已轉發 wxpmaterialBillModel modelEditToSource = new wxpmaterialBillModel() { SourceID = this.sourceSourceID, MaterialCode = this.sourceMaterialCode, IsTranspond = "1", ActionName = "SetIsTranspond", }; this.wxpService.UpdateMaterial(new List<wxpmaterialBillModel>() { modelEditToSource });
如上圖所示,這是實現圖文素材從公衆號A複製至公衆號B裏的代碼,這種狀況下,我天天在家裏打開電腦時,都要在IP白名單中從新設置下電腦的公網IP,否則,會提示post
改爲下面的代碼就能夠輕鬆解決,我把程序部署到阿里雲上,so easy,換個思路,又是一片新的天空。this
copymaterialQueryModel copymaterialParamModel = new Base.Models.copymaterialQueryModel() { sourceSourceID = this.sourceSourceID, sourceMaterialCode = this.sourceMaterialCode, targetSourceID = this.targetSourceID, title = title, digest = digest, author = author, targetSourceUrl = targetSourceUrl, wxAccessToken = wxAccessToken }; UploadMaterialApiResultModel modelResult = WebAPIHelper.WebAPIPostData<UploadMaterialApiResultModel>(ControllerName.WXPublicApi, WXPublicApiActionName.CopyMaterial.ToString(), copymaterialParamModel);
最後,發佈一個彩蛋,作自媒體的能夠看看阿里雲