小Q的博客

  • 首页
  • net编程
    • 产品和框架
    • 开发实例
    • 经验技巧
    • 开源组件
  • wp独立站
  • 自媒体
  • 日记本
  • 工具箱
每个程序员,都应该有一个自己的博客站
  1. 首页
  2. 自媒体
  3. 正文

2个公众号之间,如何实现图文复制?

2023年8月6日 1778点热度 0人点赞 0条评论

假如你有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 });
}

 

标签: IP白名单 公众号 复制图文 获取令牌
最后更新:2023年8月6日

小Q

80后中年不油腻大叔,喜欢编写代码、打羽毛球、做木制玩具。目前定居浙江杭州

打赏 点赞
< 上一篇

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2022 小Q的博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙ICP备2022019157号-2