Skip to content
linvi edited this page May 24, 2016 · 9 revisions

Overview

Twitter allows developers to upload files on their servers so that they can be used in tweets. Twitter has a limited file format that they support:

  • Images : PNG, JPEG, WEBP and GIF. Animated GIFs are supported.
  • Videos : MP4

If you want to learn more about the supported formats and video recommendations visit Twitter upload documentation.

Let's code

Tweetinvi offers various solution to upload files on Twitter. A successful upload will create an IMedia that can be attached to a tweet publication.

Upload Images

var image = File.ReadAllBytes("path");
var media = Upload.UploadImage(image);

Upload Videos

IMPORTANT : Videos can only be published using the chunked upload endpoint.

var video = File.ReadAllBytes("path");
var media = Upload.UploadVideo(video);

Publish Tweet with media

To include a media to a Tweet simply set the Medias property.

var tweet = Tweet.PublishTweet("hello", new PublishTweetOptionalParameters
{
    Medias = { media }
});

Upload Status (Video)

When a video is uploaded, Twitter will take some time to process it. If you try to add the media as soon as you've finished uploading it, you might end up with no video attached to it.

Upload status give you information about the state of the processing on the Twitter servers.

var binary = File.ReadAllBytes(@"video_path");
var media = Upload.UploadVideo(binary);
var status = Upload.GetMediaStatus(media);

if (status.ProcessingInfo.State == "succeeded")
{
    // You can now publish your tweet
}

Chunked Uploads

Chunked upload allows developer to upload videos but it more specifically allow developers to upload any binary in multiple steps.

For phone app developers such a feature can be incredibly useful as you will no longer have to wait for an entire file to be uploaded at once.

var uploader = Upload.CreateChunkedUploader();

// Initialize the upload
var initSucceeded = uploader.Init("video/mp4", totalBinaryLength);

// Upload your different chunks
var success1 = uploader.Append(firstChunk);

if (!success1) 
{
     RetryUpload(uploader, firstChunk);
}

var success2 = uploader.Append(secondChunk);

// When you have uploaded all your chunks
var media = uploader.Complete();

References

Clone this wiki locally