[Python] Tweepy - Your media IDs are invalid

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am coding in python a bot to publish in X some graphics. I've tried 100 ways but i don't get it.... The authentication goes fine and also (or so I think) upload the image with 'media_upload' but when I run 'create_tweet' always the same error:

    The media ID is: 1895739426753396736
    Error Twitter: 400 Bad Request
    Your media IDs are invalid.


    That's the code:

    #V1.1 Twitter API Authentication
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN,ACCESS_TOKEN_SECRET,)
    api = tweepy.API(auth)

    #V2 Twitter API Authentication
    client = tweepy.Client(
    consumer_key=CONSUMER_KEY,
    consumer_secret=CONSUMER_SECRET,
    access_token=ACCESS_TOKEN,
    access_token_secret=ACCESS_TOKEN_SECRET,
    )

    def publicate_twitter(tweet_text):
    try:
    media_path = "C:\\Users\\jonca\\Documents\\Python_Files\\final_graph.png"

    # Upload image with API v1.1
    media_id = api.media_upload(filename=media_path).media_id_string
    print("The media ID is: ", media_id)

    # Create with API v2
    client.create_tweet(text=tweet_text, media_ids=[media_id])
    print("Tweeted!")

    except Exception as e:
    print(f"Error Twitter: {e}")

    Continue reading...

Compartilhe esta Página