2017-07-29 41 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 });