Download an image included in a Tweet

Apr 13, 2014 at 3:04 PM
Is it possible to download an image included in a tweet ?
Please i need an example.
Apr 14, 2014 at 3:32 PM
var tweet = Tweet.GetTweet(454952190915137536);
var imageURL = tweet.Entities.Medias.First().MediaURL;
new WebClient().DownloadFile(imageURL, @"c:\image.jpg");
Marked as answer by linvi on 4/14/2014 at 3:45 PM
Apr 14, 2014 at 10:45 PM
Imlokesh gave you the right answer.
Nothing more complicated that that.

May 2, 2014 at 3:11 PM
Thanks a lot. it's perfect.
Jun 24, 2014 at 3:03 AM
What if the media URL requires authentication, as is the case with an entity in a direct message? I believe you can use TwitterAccessor.ExecuteQuery to retrieve the entity as a string and then save that data to a file, but that doesn't seem very efficient. Is there a better way to pass authentication headers to a method like DownloadFile?
Jun 30, 2014 at 7:04 PM
Hi there,

Well that might be possible to pass the authentication headers but you would still need to know the URL that you want to request?
Maybe I am not understanding your question?

Nov 2, 2014 at 8:05 AM
Edited Nov 2, 2014 at 8:06 AM
I had the same issue and resolved it as follows

You need to deal with the HttpWebRequest directly rather than via a WebClient and you need to use some helper functionality from TweetInvi to authenticate the request

The full method (to which you can pass the MediaUrl from a "Message.MessageDTO.Entities.Medias" contained within a direct message looks like
private static async Task<byte[]> GetURLContentsAsync(string url)
            var content = new MemoryStream();

            var requestGenerator = new Tweetinvi.WebLogic.OAuthWebRequestGenerator(new Tweetinvi.WebLogic.WebHelper(), new Tweetinvi.Logic.Helpers.TwitterStringFormatter());
            var headers = requestGenerator.GenerateParameters(Tweetinvi.TwitterCredentials.ApplicationCredentials);
            var webRequest = requestGenerator.GenerateWebRequest(url, Tweetinvi.Core.Enum.HttpMethod.GET, headers);

            //var webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.CookieContainer = new CookieContainer();
            using (WebResponse response = await webRequest.GetResponseAsync())
                using (Stream responseStream = response.GetResponseStream())
                    await responseStream.CopyToAsync(content);

            return content.ToArray();
Hope it helps someone

Steve Hayles