假如你有2个公众号,想把号A的图文转到号B上,腾讯提供的转移功能。但如果想让2个号同时保留,只实现图文的复制,就要用API实现了。
具体的API名称是:草稿箱中的新建草稿。简单说,就是先在源公众号中获取源草稿(GetDraft)的数据,包括:标题、正文、作者、缩略图等,再新建草稿(AddDraft)到目标公众号。
这里有个需要注意的地方,调用API需要先获取AccessToken的令牌,这个API调用时是需要客户端的IP地址,必须要加到后台配置的IP白名单里的,这个操作可以云上操作。
下面提供了复制图文的完整代码,直接用即可
public void CopyMaterial(string sourceMaterialCode, NewsItemModel sourceNewsItem, string sourceSourceID, string localCategory, string targetSourceID, string targetSourceUrl, List<wxpapiaccountBillModel> listApiAccount) { string sourceThumbMediaId = sourceNewsItem.thumb_media_id; string content = sourceNewsItem.content; string title = sourceNewsItem.title; string author = sourceNewsItem.author; string digest = sourceNewsItem.digest; if (string.IsNullOrEmpty(targetSourceUrl)) { //设置targetSourceUrl string urlTemplate = "http://others.navisoft.com.cn/Entertain/ItemList?sourceCode={0}&categoryCode={1}&pageIndex=1"; targetSourceUrl = string.Format(urlTemplate, targetSourceID, localCategory); } //3,需先将封面上传至目标公众号 //3.1,下载原公众号中封面图片.判断是否存在?不存在,则下载 //注:需判断文件夹是否存在?若不存在,则创建 string folderName = NSCurrentAppInfo.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.GetAccessToken(sourceSourceID); Stream imageStream = this.GetMaterial(sourceAccessToken, sourceThumbMediaId); Image image = Image.FromStream(imageStream); Bitmap bitmap = new Bitmap(image); bitmap.Save(fileFullName); } //3.2,再上传至目标公众号 //此处是上传封面图片 //string targetAccessToken = this.GetAccessToken(targetSourceID); string targetAccessToken = this.GetAccessTokenByWebAPI(targetSourceID, targetSourceID); UploadMaterialApiResultModel modelApiResult1 = this.UploadMaterialAny(targetAccessToken, new wxpmaterialBillModel() { IsTemp = "0", FileFullName = fi.FullName, MaterialType = MaterialType.thumb.ToString() }); string targetThumbMediaId = modelApiResult1.media_id; //3.3,加工数据 wxpapiaccountBillModel modelSourceApiAccount = listApiAccount.FirstOrDefault(c => c.SourceID == sourceSourceID); wxpapiaccountBillModel modelTargetApiAccount = listApiAccount.FirstOrDefault(c => c.SourceID == targetSourceID); content = content.Replace(modelSourceApiAccount.SourceCode, modelTargetApiAccount.SourceCode); content = content.Replace(modelSourceApiAccount.SourceName, modelTargetApiAccount.SourceName); //4,上传素材至目标公众号 UploadNewsModel modelUpload = new UploadNewsModel() { title = title, author = author, digest = digest, content = content, content_source_url = targetSourceUrl, show_cover_pic = "0", thumb_media_id = targetThumbMediaId }; UploadMaterialApiResultModel modelResult = this.AddDraft(targetAccessToken,new List<UploadNewsModel>() { modelUpload }); //4.1,需标记源公众号中此素材已转发 wxpmaterialBillModel modelEditToSource = new wxpmaterialBillModel() { SourceID = sourceSourceID, MaterialCode = sourceMaterialCode, IsTranspond = "1", ActionName = "SetIsTranspond", }; this.UpdateMaterial(new List<wxpmaterialBillModel>() { modelEditToSource }); }
文章评论