当前位置:首页 » 电脑基础知识

解决微信公众平台IP白名单

2017-12-02 07:40 本站整理 浏览(4)

微信公众平台,作为自媒体的旗舰级产品,越来越多的人已经投入它的怀抱。正如它的广告词所说:再小的个体,也有品牌

好吧,闲话不多说,今天要说的是它的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.产品.登录器(永久免费)