1. 程式人生 > >解決微信公眾平臺IP白名單

解決微信公眾平臺IP白名單

borde splay 轉發 電信 dir count 可能 白名單 direct

微信公眾平臺,作為自媒體的旗艦級產品,越來越多的人已經投入它的懷抱。正如它的廣告詞所說:再小的個體,也有品牌

好吧,閑話不多說,今天要說的是它的IP白名單機制。

技術分享圖片

技術分享圖片

我們現在安裝的大部分都是電信的家庭寬帶,它的公網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,不然,會提示

技術分享圖片

改成下面的代碼就可以輕松解決,我把程序部署到阿裏雲上,so easy,換個思路,又是一片新的天空。

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);

最後,發布一個彩蛋,做自媒體的可以看看

Navi.Soft31.微信WinForm框架(含下載地址)  

Navi.Soft31.產品.登錄器(永久免費)

解決微信公眾平臺IP白名單