python - 从 Twitter API 2.0 获取 user.fields 时出现问题

标签 python json twitter

我想从 Twitter API 2.0 端点加载推文,并尝试获取标准字段(作者、文本……)和一些扩展字段,尤其是。用户.字段。 端点和参数的定义工作没有错误。 在生成的 json 中,我只找到标准字段,但没有找到所需的 user.fields(用户名、指标)。

示例代码片段:

from datetime import datetime, timedelta
import requests
import json
import pandas as pd

# read bearer token for authentication
with open('bearer_token.txt') as fp:
    BEARER_TOKEN = fp.read()

query_string = '("TSLA") (lang:en)'
    
# setup the API request
endpoint = 'https://api.twitter.com/2/tweets/search/recent'
headers = {'authorization': f'Bearer {BEARER_TOKEN}'}
params = {
    'query': query_string,
    'max_results': '100',
    'tweet.fields': 'created_at,lang,text,author_id,public_metrics',
    # that doesn't work
    'user.fields': 'name,username,public_metrics'
    #'expansions': 'attachments.media_keys'
}

response = requests.get(endpoint,
                            params=params,
                            headers=headers)  # send the request

print(json.dumps(response.json(), indent=3, sort_keys=True))

生成的 json 显示了这一点(其中我缺少用户字段:名称、用户名、public_metrics):

    {
   "data": [
      {
         "author_id": "33286321",
         "created_at": "2021-03-13T16:25:02.000Z",
         "id": "1370772902769999874",
         "lang": "en",
         "public_metrics": {
            "like_count": 0,
            "quote_count": 0,
            "reply_count": 0,
            "retweet_count": 0
         },
         "text": "Try our Option Swing Trading service built for individuals who want to trade around their full-time careers. Take a free 10-day trail No Credit Card  $AAPL $TSLA $FB $MSFT"
      },
      {
         "author_id": "1142453041364385794",
         "created_at": "2021-03-13T16:24:41.000Z",
         "id": "1370772813469130756",
         "lang": "en",
         "public_metrics": {
            "like_count": 0,
            "quote_count": 0,
            "reply_count": 0,
            "retweet_count": 28
         },
         "text": "RT @Stalingrad_Poor: With bitcoin trading at $60k, TSLA has now made more money on its bitcoin investment and selling regulatory credits th\u2026"
      },
      {
         "author_id": "1349496824650989568",
         "created_at": "2021-03-13T16:24:35.000Z",
         "id": "1370772791411245056",
         "lang": "en",
         "public_metrics": {
            "like_count": 0,
            "quote_count": 0,
            "reply_count": 0,
            "retweet_count": 3
         },
         "text": "RT @VolaarRide: $OZSC By teaming up with the GEMM Network, we can now provide our customers with additional services such as Project Financ\u2026"
      },

问题: 我需要做什么才能在我的回复中获取这些信息?

最佳答案

根据documentation ,要使用 user.fields 参数,您必须在 expansions 参数中包含 author_id

关于python - 从 Twitter API 2.0 获取 user.fields 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66615958/

相关文章:

使用 Py.Test 进行单元测试的 Python Mocking

python - App Engine - 难以访问 Twitter 数据 - Flask

python - 尝试从 python 中的 json 响应解析值

javascript - 检查 JSON 对象中是否存在值

json - 如何为 API 客户端提供端点的最新验证规则?

node.js - 推特 API, "Unable to verify your credentials."

javascript - Twitter JavaScript OAuth : Get Access Token without Server Side

python - list() 给出类型错误 : 'Node' object is not callable - BUT ONLY AFTER SOME CODES

Python - Apache Beam - Flink 运行器设置 : ReadFromKafka returns error - RuntimeError: cannot encode a null byte[]

python - python 内置的 __exit__ 参数类型是什么?