skip to Main Content

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']}")

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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 = self.get_twitter_conn_v1(api_key, api_secret, access_token, access_token_secret)
    client_v2 = self.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!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search