python - Tweepy 在使用 Twitter API v2 方法时不返回 url 媒体字段

标签 python python-3.x twitter tweepy

我正在请求 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

关于python - Tweepy 在使用 Twitter API v2 方法时不返回 url 媒体字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71293546/

相关文章:

Python:进程中线程中队列处理的怪异

python - 使用参数时无法使用 PyMySQL 插入数据

python - 编程距离

django - 如何安装 libpq-fe.h?

python - Twitter 流媒体 : Get trends on Twitter on the basis of countries

python - 如何使用带有权重的预定义/训练(hdf5)文件来预测一类新的脑电图数据?

python - dask 数组上的数组操作

python - 子图边距以适合 matplotlib 中的标题和标签

python - Django Twitter OAuth 身份验证

api - Tweepy 流媒体和 Tweepy 搜索之间的区别