【问题标题】:Twitter API 2.0 - Unable to fetch user.fieldsTwitter API 2.0 - 无法获取 user.fields
【发布时间】:2021-08-23 00:23:53
【问题描述】:

我使用的是 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'
  }]
}

如您所见,没有返回以下信息:nameusernamelocation

知道如何检索此信息吗?

【问题讨论】:

    标签: twitter twitterapi-python


    【解决方案1】:

    您的查询确实返回了正确的数据。我自己测试过。

    完整的示例响应结构如下:

    {
      "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 对象中(在您的情况下为nameusernamelocation)。对应的user对象可以通过author_id字段引用。

    【讨论】:

    • 我知道了,谢谢。我只是在我的代码中刮掉数据:)。知道包含的内容。
    猜你喜欢
    • 2021-06-11
    • 2015-08-27
    • 2022-11-11
    • 2022-01-19
    • 2022-12-07
    • 2017-01-22
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    相关资源
    最近更新 更多