Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Add An Image To A Tweet With TwitterAPI?

I'm struggling to find documentation on how to add an image to a Tweet through Python's TwitterAPI. Any ideas?

Here's what I have so far:

consumer_key = ' '
consumer_secret = ' '
access_token_key = ' '
access_token_secret = ' '

from TwitterAPI import TwitterAPI

api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret)
file = open('image.jpg', 'rb')
data = file.read()
r = api.request('statuses/update_with_media', {'status':'Your tweet'}, {'media[]':data})
print(r.status_code)

Output:

Traceback (most recent call last):
File "tweet_media.py", line 48, in <module>
r = api.request('statuses/update_with_media', {'status':'Your tweet'}, {'media[]':data})
TypeError: request() takes at most 3 arguments (4 given)
like image 718
Sam Perry Avatar asked Dec 23 '13 22:12

Sam Perry


People also ask

How do you attach an image to a Tweet?

Tap the camera icon to add a new photo or video to your Tweet. Tap the photo icon to attach an existing photo, video, or GIF. Once you have 2 or more photos selected, you can tap and hold a photo to drag and reorder before Tweeting. Tip: Read about how to include a video in your Tweet.

Why can't I add a picture to my Tweet?

Supported Formats. Images uploaded to Twitter must be smaller than 3MB in size and saved as a GIF, JPEG or PNG file. The BMP, TIFF and animated GIF file formats are not supported by Twitter. Note that you can only upload one image per tweet, whether you are using the main Twitter website or a third-party application.

Can you upload JPEG to Twitter?

Check your file type.Twitter supports JPEG, GIF, and PNG file formats.

How do you post pictures on Tweepy?

The media_upload() method of the API class in Tweepy module is used to upload a media object to Twitter. Parameters : filename : path of the media file to be uploaded. file : a file object to be uploaded.


1 Answers

This example will upload a tweet with an embedded image. You will need to use TwitterAPI 2.1.8.7 or higher.

from TwitterAPI import TwitterAPI

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN_KEY = ''
ACCESS_TOKEN_SECRET = ''

api = TwitterAPI(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
file = open('Your_image.png', 'rb')
data = file.read()
r = api.request('statuses/update_with_media', {'status':'Your tweet'}, {'media[]':data})
print(r.status_code)
like image 167
Jonas Avatar answered Sep 19 '22 16:09

Jonas