How to post a tweet with media (picture) using Twitter API V2 and Tweepy Python?
Asked Answered
V

2

9

i'm trying to create my first Twitter bot using Twitter API V.2 and Tweepy. So i can post simple text Tweets but i haven't found how to post tweets with medias (pictures) so how can i do that ? (I saw that some people say "you can't post media tweets using twitter API v2... You need to use API V 1.1 " so if it's true how can i use Twitter API V1.1 instead of API V2 ? )

Thank you if you can help me ^^

Here is my actual code :

from io import BytesIO
from PIL import Image
import tweepy
from tweepy import API

consumer_key = "APP_KEY"
consumer_secret = "APP_SECRET_KEY"
access_token = "TOKEN"
access_token_secret = "SECRET_TOKEN"

client = tweepy.Client(
 consumer_key=consumer_key,
 consumer_secret=consumer_secret,
 access_token=access_token,
 access_token_secret=access_token_secret
)

response = client.create_tweet(
  text="Just a dummy tweet",
  # in_reply_to_tweet_id= 1484105392598749186 <--- Reply to a tweet by ID

)

print(f"https://twitter.com/user/status/{response.data['id']}")
Velarde answered 28/1, 2022 at 9:50 Comment(1)
can you please help with a similar problem? #76352878Froghopper
B
16

For the v2 Free version, tweepy.API (v1.1) does not have permission to send tweets anymore (at least in my case), but can still upload media and retrieve media_id.

What you can do is use both v1.1 and v2 (tweepy.Client) for this.

The code is following:

def get_twitter_conn_v1(api_key, api_secret, access_token, access_token_secret) -> tweepy.API:
    """Get twitter conn 1.1"""

    auth = tweepy.OAuth1UserHandler(api_key, api_secret)
    auth.set_access_token(
        access_token,
        access_token_secret,
    )
    return tweepy.API(auth)

def get_twitter_conn_v2(api_key, api_secret, access_token, access_token_secret) -> tweepy.Client:
    """Get twitter conn 2.0"""

    client = tweepy.Client(
        consumer_key=api_key,
        consumer_secret=api_secret,
        access_token=access_token,
        access_token_secret=access_token_secret,
    )

    return client

client_v1 = get_twitter_conn_v1(api_key, api_secret, access_token, access_token_secret)
client_v2 = get_twitter_conn_v2(api_key, api_secret, access_token, access_token_secret)

media_path = "path_to_media"
media = client_v1.media_upload(filename=media_path)
media_id = media.media_id

client_v2.create_tweet(text="Tweet text", media_ids=[media_id])

Hope it helps!

Blackett answered 23/6, 2023 at 19:4 Comment(4)
This worked well for me, I just had to remove the "self." from the get_connections. Not sure what that was for.Corollary
ah nice, it was just a part of the Class before for me, so forgot to remove self. in the example, edited the answer, thanksBlackett
@Blackett Thank you so much for this. You saved me a lot of headache!Spectrogram
what happens when you fire your whole company? thisScouring
E
7

If you are managing an Essential project, you need to apply for Elevated (it's free) in your project located on your Twitter Developer Portal, this is because an Elevated project can use both v1.1 and v2 (Essential only v2).

Once your submission is accepted, you must verify to allow read and write functions on your project (Developer Platform -> Project & Apps -> YOUR_PROJECT -> Development -> user authentication settings) selecting "OAuth 1.0a" and selecting "Read and Write".

Now, you can tweet media with the following code:

    auth = tweepy.OAuth1UserHandler(
       consumerKey,
       consumerSecret,
       accessToken,
       accessTokenSecret
    )

    api = tweepy.API(auth)

    media = api.media_upload(filename="./assets/twitter-logo.png")
    print("MEDIA: ", media)

    tweet = api.update_status(status="Image upload", media_ids= 
    [media.media_id_string])
    print("TWEET: ", tweet)

For uploading media, you need to use the function media_upload (update_status_with_media is deprecated). This functions returns an Media object that have the attribute media_id_string, which is the element you need to make a tweet with a pic.

Edmonson answered 3/3, 2022 at 18:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.