2017-07-29 63 views
0

我试图推销一个图像几个小时了,我开始怀疑它是不是在单声道上工作?TweetSharp SendTweetWithMedia在MacOS和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 }); 
      } 
     } 
    } 

回答

1

对于Twitter的图片有很多限制。检查Twitter.com上的API文档。图像必须低于某个字节(文件)大小,并且每个维度中的像素数少于某个像素数。另外只允许某些文件格式。

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

最后,该Api方法现在已被弃用。您应该使用uploadmedia,然后使用sendtweet方法和媒体ID列表。原来的tweetsharp(现已放弃)不支持这个,但TweetMoaSharp会。

+0

谢谢。这可能是导致问题的被遗弃的API。我切换到TweetMoaSharp,它的功能就像一个魅力。 –

0

请到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 });