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)
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.
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.
Check your file type.Twitter supports JPEG, GIF, and PNG file formats.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With