解决微信公众平台IP白名单
微信公众平台,作为自媒体的旗舰级产品,越来越多的人已经投入它的怀抱。正如它的广告词所说:再小的个体,也有品牌
好吧,闲话不多说,今天要说的是它的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,换个思路,又是一片新的天空。
1 2 3 4 5 6 7 8 9 10 11 12 | 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框架(含下载地址)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库