简体   繁体   中英

TweetSharp SendTweetWithMedia on MacOS and Mono

I'm trying to tweet a image for a few hours now, and I'm starting to wonder if it's simply not working on Mono?

The code I use is this:

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

I've read dozens if sample code around the net and all of them are very similar to mine. Am I missing something obvious?

No image is uploaded, and tweet is null.

Edit: As Yort pointed out, the API call is deprecated and Tweetsharp doesn't support the new way. So I switched to TweetMoaSharp instead. The way I solved it using TweetMoaSharp is:

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

There are a lot of restrictions on images for Twitter. Check the API docs on Twitter.com. The image must be below a certain byte (file) size, as well as less than a certain pixel count in each dimension. Additionally only certain file formats are allowed.

Also check the Response property of the TwitterService object after making the call to see if that provides more details for the error.

Finally, that Api method is now deprecated. You should use uploadmedia and then the sendtweet method with a list of media ids. Original tweetsharp (now abandoned) doesn't support this but TweetMoaSharp does.

Please go to https://apps.twitter.com/ , login to your account, then go to Keys and Access Tokens tab and make sure you have created your api keys, tokens etc.

Please make sure you created your tweeter service correctly→

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

then you can use without problems→

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM