NotADev
NotADev

Reputation: 41

Code to Automatically Post Images To My Twitter Account via Python Script

I want to post images programmatically on my own (not other peoples') twitter account. To that end, I tried using the tweepy library -- but they have a method called api.update_with_media('the image') which does not work.

It seems twitter has deprecated this method: ...statuses/update_with_media (sorry for the truncated html -> I'm new here, so they won't let me post too many links)

Twitter instructs us to use this instead: ...public/uploading-media

Basically, this is a 2-step method. Step 1) Upload media and get some ID in json Step 2) Use this ID to update a status

Both of these steps require authentication. The problem is that the twitter libraries, (which can be found here twitter-libraries) don't actually support this new method. They all have the simple one from before in their documentation.

It seems possibly this one fixed it, but the docs didn't get updated (I didn't actually play with it too deeply -- it is at this point that I decided to contract this out). It can be found here.

It seems the options are:

1) build the code from scratch, including a) the OAuth (breakable, complex) b) the update status script (re-writing code the libraries already have)

2) Same as above but use a generic OAuth library

3) Find a library that supports the new upload method -- there may very well be one in that list, but it would involve reading through documentation and testing, or

4) Some other idea I didn't think of

I made sure this works for text updates -- just replace api.update_with_media(image) with api.update_status(status='Hey').

For reference, here's the code I'm using:

` import urllib import tweepy

@app.route('/') def testing():

consumer_key = '---'
consumer_secret = '---'
access_token = '---'
access_token_secret = '---'

#OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

#Creation of the actual interface, using authentication
api = tweepy.API(auth)

#should encode the image - I tried this several different way
image = urllib.urlopen('image-url')

try:
    api.update_with_media(image)
except Exception, e:
    print e

return "OK"

`

Upvotes: 3

Views: 2240

Answers (1)

NotADev
NotADev

Reputation: 41

I solved it by using this library instead: http://geduldig.github.io/TwitterAPI/examples.html

Upvotes: 1

Related Questions