Recently, the twitter developer API has not shown an option to apply for Elevated Access. This means I am stuck with the free version of API v2. Now, I am trying to post a tweet with some text as well as an image. There is a parameter in create_tweet() called media_ids, but the problem is that there is no media_upload() for me to get a media id. I assume this is due to API changes, more specifically, the deprecation of Elevated Access. How would I be able to post text and an image form a path? Here is my Python code:
import tweepy
import keys # This is my python script that contains my dev keys.
client = tweepy.Client(consumer_key=keys.api_key,
consumer_secret=keys.api_secret,
access_token=keys.access_token,
access_token_secret=keys.access_token_secret,
bearer_token=keys.bearer_key)
def tweet(client: tweepy.Client, message: str, media_path=None):
client.create_tweet(text=message)
# How would I upload media ^^^ here from a path in the current directory?
print("Tweet tweet!")
if __name__ == "__main__":
my_message = "I love cats and dogs!"
tweet(client=client, message=my_message, media_path="pets.png")
PS: I am using the latest version of tweepy and python3 respectably.
I looked online to see if there were any problems with applying for Elevated Access, or if it was just something on my end. I could not find anything substantive enough, so I came here.
I found this code earlier today from another user. Works perfectly for me
import tweepy
from tweepy import API
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
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(consumer_key, consumer_secret, access_token, access_token_secret)
client_v2 = get_twitter_conn_v2(consumer_key, consumer_secret, access_token, access_token_secret)
media_path = "C:\\YourPath"
media = client_v1.media_upload(filename=media_path)
media_id = media.media_id
client_v2.create_tweet(text="This is a test", media_ids=[media_id])
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