I am requesting Twitter API v2 to get the Tweet's details and using a Client object which is required to authenticate.
import tweepy, config
client = tweepy.Client(bearer_token=config.BEARER_TOKEN, consumer_key=config.API_KEY, consumer_secret=config.API_SECRET, access_token=config.ACCESS_TOKEN,access_token_secret=config.ACCESS_TOKEN_SECRET,wait_on_rate_limit=False)
x = client.get_tweet(id =1490880839844233218,tweet_fields="lang", expansions = ["author_id","attachments.media_keys"], media_fields = ["type","url","media_key"] )
print(x)
But I don't get the url media field.
Response(data=<Tweet id=1490880839844233218 text=Android 12's February security patches are ready to rollout! https://mobile.twitter.com/EvolutionXROM/status/1490880839844233218>, includes={'media': [<Media media_key=3_1490880836576550914 type=photo>], 'users': [<User id=1106906709786656768 name=Evolution X username=EvolutionXROM>]}, errors=[], meta={})
But if I request the same thing with curl:
curl --request GET 'https://api.twitter.com/2/tweets/1490880839844233218?&tweet.fields=lang&expansions=author_id,attachments.media_keys&media.fields=media_key,type,url' --header 'Authorization: Bearer $TOKEN'
I get this response.
{"data":{"lang":"en","author_id":"1106906709786656768","text":"Android 12's February security patches are ready to rollout! https://mobile.twitter.com/EvolutionXROM/status/1490880839844233218","attachments":{"media_keys":["3_1490880836576550914"]},"id":"1490880839844233218"},"includes":{"media":[{"media_key":"3_1490880836576550914","type":"photo","url":"https://pbs.twimg.com/media/FLCsOMqVIAI_Emf.jpg"}],"users":[{"id":"1106906709786656768","name":"Evolution X","username":"EvolutionXROM"}]}}
As you can see here, if I request using curl, the media url appears as a media field.
Even though I found a workaround using the Twitter API v1 get_status
method, I want to get this to work using the latest API v2.
With Twitter API v1 :
import tweepy, config
auth = tweepy.OAuth2BearerHandler(bearer_token = config.BEARER_TOKEN)
api = tweepy.API(auth=auth)
status = api.get_status(tweet_id,tweet_mode='extended')
print(status)
Response :
Status(_api=<tweepy.api.API object at 0x71bd8b3580>, _json={'created_at': 'Tue Feb 08 02:51:06 0000 2022', 'id': 1490880839844233218, 'id_str': '1490880839844233218', 'full_text': "Android 12's February security patches are ready to rollout! https://mobile.twitter.com/EvolutionXROM/status/1490880839844233218", 'truncated': False, 'display_text_range': [0, 60], 'entities': {'hashtags': [], 'symbols': [], 'user_mentions': [], 'urls': [], 'media': [{'id': 1490880836576550914, 'id_str': '1490880836576550914', 'indices': [61, 84], 'media_url': 'http://pbs.twimg.com/media/FLCsOMqVIAI_Emf.jpg', 'media_url_https': 'https://pbs.twimg.com/media/FLCsOMqVIAI_Emf.jpg',......
I am not posting the full response here because it's too large.
CodePudding user response:
If you are simply printing the objects and looking at that output, the string representations of API v2 models/objects only include the default attributes that are guaranteed to exist.
The objects themselves still include the relevant data, which you can access as attributes or by key, like a dictionary.