【问题标题】:TweetSharp SendTweetWithMedia on MacOS and MonoMacOS 和 Mono 上的 TweetSharp SendTweetWithMedia
【发布时间】:2018-01-05 16:47:20
【问题描述】:

我现在尝试在推特上发布几个小时的图片,但我开始怀疑它是否根本无法在 Mono 上运行?

我使用的代码是这样的:

    public void SendMediaTweet(string Reply, string ImagePath)
    {
        if (File.Exists(ImagePath))
        {
            using (var stream = new FileStream(ImagePath, FileMode.Open))
            {
                Dictionary<string, Stream> images = new Dictionary<string, Stream> { { ImagePath, stream } };
                var tweet = Service.SendTweetWithMedia(new SendTweetWithMediaOptions
                {
                    Status = Reply,
                    Images = images
                });
            }
        }
    }

我已经阅读了几十个网络上的示例代码,所有这些都与我的非常相似。我错过了什么明显的东西吗?

没有上传图片,推文为空。

编辑: 正如 Yort 指出的那样,API 调用已被弃用,Tweetsharp 不支持新方法。所以我改用 TweetMoaSharp。我使用 TweetMoaSharp 解决它的方法是:

    public void SendMediaTweetReply(string Reply, long StatusId, string ImagePath)
    {
        if (File.Exists(ImagePath))
        {
            using (var stream = new FileStream(ImagePath, FileMode.Open))
            {
                var Media = Service.UploadMedia(new UploadMediaOptions() { Media = new MediaFile() { FileName = ImagePath, Content = stream } });

                List<string> MediaIds = new List<string>();
                MediaIds.Add(Media.Media_Id);

                var Result = Service.SendTweet(new SendTweetOptions() { Status = Reply, InReplyToStatusId = StatusId, MediaIds = MediaIds });
            }
        }
    }

【问题讨论】:

    标签: c# mono tweetsharp


    【解决方案1】:

    Twitter 的图片有很多限制。查看 Twitter.com 上的 API 文档。图像必须小于某个字节(文件)大小,以及每个维度中的某个像素数。此外,仅允许某些文件格式。

    在调用之后还要检查 TwitterService 对象的 Response 属性,看看它是否提供了错误的更多详细信息。

    最后,该 Api 方法现已弃用。您应该使用uploadmedia,然后使用带有媒体ID 列表的sendtweet 方法。原始的 tweetsharp(现已弃用)不支持此功能,但 TweetMoaSharp 支持。

    【讨论】:

    • 谢谢。可能是废弃的 API 导致了这个问题。我切换到 TweetMoaSharp,它就像一个魅力。
    【解决方案2】:

    请转到https://apps.twitter.com/,登录您的帐户,然后转到Keys and Access Tokens 选项卡并确保您已创建您的api 密钥、令牌等。

    请确保您正确创建了高音扬声器服务→

    TwitterService _ts = new TwitterService(consumerKey, consumerSecret, token, tokenSecret);
    

    那么你就可以毫无问题地使用了→

    var bytes = File.ReadAllBytes(@"c:/tmp/a.bmp");
    var d = new Dictionary<string, Stream> { { "x", new MemoryStream(bytes) } };
    _ts.SendTweetWithMedia(new SendTweetWithMediaOptions { Images = d });
    

    【讨论】:

      猜你喜欢
      • 2013-05-02
      • 2023-03-22
      • 1970-01-01
      • 2021-02-03
      • 2015-12-09
      • 2015-08-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多