问题描述
我使用的是 API 2.0 版,但无法获取 user.fields
结果。所有其他参数似乎都正确返回结果。我正在关注this documentation。
url = "https://api.twitter.com/2/tweets/search/all"
query_params = {
"query": "APPL","max_results": "10","tweet.fields": "created_at,lang,text,author_id","user.fields": "name,username,created_at,location","expansions": "referenced_tweets.id.author_id",}
response = requests.request("GET",url,headers=headers,params=query_params).json()
示例结果:
{
'author_id': '1251347502013521925','text': 'All conspiracy. But watch for bad news on Apple. Such a vulnerable stocktechnically for the biggest market cap @ $2.1T ( Thanks Jay). This is the glue for the bulls. But,they stopped innovating when Steve died,built a fancy office and split the stock. $appl','lang': 'en','created_at': '2021-06-05T02:33:48.000Z','id': '1401004298738311168','referenced_tweets': [{
'type': 'retweeted','id': '1401004298738311168'
}]
}
如您所见,不会返回以下信息:name
、username
和 location
。
知道如何检索此信息吗?
解决方法
您的查询确实返回了正确的数据。我自己测试过。
完整的示例响应结构如下:
{
"data": [
{
"created_at": "2021-06-05T02:33:48.000Z","lang": "en","id": "1401004298738311168","text": "All conspiracy. But watch for bad news on Apple. Such a vulnerable stocktechnically for the biggest market cap @ $2.1T ( Thanks Jay). This is the glue for the bulls. But,they stopped innovating when Steve died,built a fancy office and split the stock. $appl","author_id": "1251347502013521925","referenced_tweets": [
{
"type": "retweeted","id": "1401004298738311168"
}
]
}
],"includes": {
"users": [
{
"name": "Gary Casper","id": "1251347502013521925","username": "Hisel1979","created_at": "2020-07-11T13:39:58.000Z"
}
]
}
}
您提供的示例结果来自 data
对象。但是,扩展的对象数据将嵌套在 includes
对象中(在您的情况下为 name
、username
和 location
)。可以通过 user
字段引用相应的 author_id
对象。