Tweepy 在使用 Twitter API v2 方法时没有 return url 媒体字段

Tweepy does not return url media field while using Twitter API v2 methods

我正在请求 Twitter API v2 获取推文的详细信息并使用需要进行身份验证的客户端对象。

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)

但我没有得到 url 媒体字段。

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={})

但是如果我用 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'

我收到了这个回复。

{"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"}]}}

如您所见,如果我使用 curl 请求,媒体 url 将显示为媒体字段。

即使我找到了使用 Twitter API v1 get_status 方法的解决方法,我还是希望使用最新的 API v2.

来解决这个问题

使用 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)

回复:

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',......

我没有在这里发布完整的回复,因为它太大了。

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.

https://tweepy.readthedocs.io/en/v4.6.0/faq.html#why-am-i-not-getting-expansions-or-fields-data-with-api-v2-using-client