-
Notifications
You must be signed in to change notification settings - Fork 219
Upload
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](https://developer.twitter.com/en/docs/media/upload-media/uploading-media/media-best-practices
).
Tweetinvi offers various solution to upload files on Twitter. A successful upload will create an IMedia that can be attached to a tweet publication.
var image = File.ReadAllBytes("path");
var media = Upload.UploadBinary(image);
IMPORTANT : Videos can only be published using the chunked upload endpoint.
var video = File.ReadAllBytes("path");
var media = Upload.UploadVideo(video);
To include a media to a Tweet simply set the Medias
property.
var tweet = Tweet.PublishTweet("hello", new PublishTweetOptionalParameters
{
Medias = { media }
});
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);
The upload is completed but it does not mean it succeeded!
if (!media.HasBeenUploaded)
{
// Something went wrong during the upload. Please retry or check the video type/settings.
return;
}
Now we know that the upload has been successful. But we need to wait for Twitter to process it! You have 2 solutions to do so:
// 1. Just await for Twitter to have processed the upload (RECOMMENDED)
Upload.WaitForMediaProcessingToGetAllMetadata(media);
// Now the media is ready to be used in a Tweet
// 2. Or you can do this process manually (NOT RECOMMENDED)
var mediaUploadStatus = Upload.GetMediaStatus(media);
while (mediaUploadStatus.ProcessingInfo.State != "succeeded")
{
Thread.Sleep(1000); // or any logic you want to use
// Request the status of the upload again
mediaUploadStatus = Upload.GetMediaStatus(media);
}
// Now the media is ready to be used in a Tweet
NOTE : Please remember that Twitter allow the STATUS endpoint to be used only for videos with a media category set to tweet_video
. Using a different media category will result in STATUS to throw a 503 exception.
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();