Extracting DM Images Over Twitter API

Extracting images from received DMs over twitter API has proven to be rather tricky and there aren’t enough information on how to do it. In this entry I’m going to explain how to achieve that programatically, using both curl as well as twitter4j.

*Note: you have to make sure that the app you are using has “Read, Write and Access direct messages” permission, other wise you’ll get a “HTTP/1.1 401 Authorization Required” error. 

Step 1: Get the Image ton URL

Within DMs images are represented as media entities, the media entity has several links, the one you need is “media_url”. the https://ton.* link

Screen Shot 2015-10-25 at 11.30.25 AM

 Step2 : Use the link to download the image 

Using Twitter4j: 

Call twitter.getDMImageAsStream(“TON URL“), this will return an input stream of the image.

Using Curl:

Put the URL in the get command with the standard twitter OAuth headers. Screen Shot 2015-10-25 at 11.38.08 AM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s