跳转到主要内容

开始使用 full-archive Post counts     endpoint

本快速上手指南将帮助你使用 Postman(一个可用于发起 HTTP 请求的图形化工具)向 full-archive Post counts endpoint 发送你的第一个请求。 如果你想查看不同编程语言的示例代码,请访问我们的 X API v2 示例代码 GitHub 仓库。

先决条件

full-archive Post counts endpoint 目前仅向拥有 Pro 或 Enterprise 访问级别的用户开放。要使用此 endpoint,你必须申请 Pro 或 Enterprise 访问权限除获得访问批准外,你还需要一组用于对请求进行身份验证的 密钥和令牌。你可以按以下步骤生成这些密钥和令牌:
  • 在开发者门户中前往具备 Enterprise 访问的 Project,并确保该 Project 下关联了一个 开发者 App
  • 前往你的 App 的“Keys and tokens”页面生成所需凭据。请确保将所有凭据保存在安全位置。

构建全量归档 Post 计数请求的步骤

步骤一:选择工具或库 你可以使用多种工具、代码示例和库来向此 endpoint 发起请求;为简化流程,我们将在此处使用 Postman。 要将 X API v2 的 Postman 集合加载到你的环境中,请点击以下按钮: 在你将 X API v2 集合加载到 Postman 后,进入 Post counts > Full-archive Post counts 请求。 步骤二:为请求进行认证 要正确地向 X API 发起请求,你需要确认自己具备相应权限。对于此 endpoint,必须使用 OAuth 2.0 App-Only 方式进行认证。 你需要将密钥和令牌,尤其是 App Access Token(亦称 App-only Bearer Token),添加到 Postman。你可以在 Postman 右上角选择名为“X API v2”的环境,并将密钥和令牌添加到 “initial value” 和 “current value” 字段(点击环境下拉菜单旁的眼睛图标)。 如果设置正确,该变量会自动注入到请求的 authorization 选项卡中。   步骤三:创建一个 query(查询) 每个全量归档 Post 计数请求都需要一个 query。本示例使用匹配由 @XDevelopers 账号发布的 Posts 的 query。我们使用 from 运算符,并将其设为 XDevelopers(不区分大小写): from:XDevelopers 在 Postman 中,进入 “Params” 选项卡,将此 ID,或用逗号分隔的一串 Post ID,输入到 ids 参数的 “Value” 列中。  
KeyValueDescription
queryfrom:XDevelopers提交给全量归档 Post 计数 endpoint 的查询
Step four (optional): 指定粒度和时间范围 在完成步骤三后,若直接点击 “Send” 按钮,你将获得默认的全量归档 Post 计数:最近 30 天内按小时统计。若要按天获取全量归档 Post 计数,需要添加 granularity 参数并将其值设为 day。若要获取早于 30 天之前的 Post 计数,需要为 start_time 和 end_time 参数设置所需的时间值。  在 Postman 中,进入 “Params” 选项卡,在 “Query Params” 表中添加以下 key:value 对:
KeyValueDescription
granularitydayPost 计数结果的粒度。可选值为 day、hour 或 minute
start_time2021-05-01T00:00:00Z返回 Posts 的最早 UTC 时间戳
end_time2021-06-01T00:00:00Z返回 Posts 的最晚 UTC 时间戳。
你现在应该能在 “Send” 按钮旁看到以下 URL: https://api.x.com/2/tweets/counts/all?query=from%3AXDevelopers&start_time=2021-05-01T00:00:00Z&end_time=2021-06-01T00:00:00Z&granularity=day 步骤五:发送请求并查看响应 完成所有设置后,点击 “Send” 按钮,你会收到类似以下的响应:
{
   "data": [
       {
           "end": "2021-05-02T00:00:00.000Z",
           "start": "2021-05-01T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-03T00:00:00.000Z",
           "start": "2021-05-02T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-04T00:00:00.000Z",
           "start": "2021-05-03T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-05T00:00:00.000Z",
           "start": "2021-05-04T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-06T00:00:00.000Z",
           "start": "2021-05-05T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-07T00:00:00.000Z",
           "start": "2021-05-06T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-08T00:00:00.000Z",
           "start": "2021-05-07T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-09T00:00:00.000Z",
           "start": "2021-05-08T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-10T00:00:00.000Z",
           "start": "2021-05-09T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-11T00:00:00.000Z",
           "start": "2021-05-10T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-12T00:00:00.000Z",
           "start": "2021-05-11T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-13T00:00:00.000Z",
           "start": "2021-05-12T00:00:00.000Z",
           "tweet_count": 6
       },
       {
           "end": "2021-05-14T00:00:00.000Z",
           "start": "2021-05-13T00:00:00.000Z",
           "tweet_count": 1
       },
       {
           "end": "2021-05-15T00:00:00.000Z",
           "start": "2021-05-14T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-16T00:00:00.000Z",
           "start": "2021-05-15T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-17T00:00:00.000Z",
           "start": "2021-05-16T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-18T00:00:00.000Z",
           "start": "2021-05-17T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-19T00:00:00.000Z",
           "start": "2021-05-18T00:00:00.000Z",
           "tweet_count": 1
       },
       {
           "end": "2021-05-20T00:00:00.000Z",
           "start": "2021-05-19T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-21T00:00:00.000Z",
           "start": "2021-05-20T00:00:00.000Z",
           "tweet_count": 8
       },
       {
           "end": "2021-05-22T00:00:00.000Z",
           "start": "2021-05-21T00:00:00.000Z",
           "tweet_count": 1
       },
       {
           "end": "2021-05-23T00:00:00.000Z",
           "start": "2021-05-22T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-24T00:00:00.000Z",
           "start": "2021-05-23T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-25T00:00:00.000Z",
           "start": "2021-05-24T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-26T00:00:00.000Z",
           "start": "2021-05-25T00:00:00.000Z",
           "tweet_count": 1
       },
       {
           "end": "2021-05-27T00:00:00.000Z",
           "start": "2021-05-26T00:00:00.000Z",
           "tweet_count": 1
       },
       {
           "end": "2021-05-28T00:00:00.000Z",
           "start": "2021-05-27T00:00:00.000Z",
           "tweet_count": 1
       },
       {
           "end": "2021-05-29T00:00:00.000Z",
           "start": "2021-05-28T00:00:00.000Z",
           "tweet_count": 2
       },
       {
           "end": "2021-05-30T00:00:00.000Z",
           "start": "2021-05-29T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-05-31T00:00:00.000Z",
           "start": "2021-05-30T00:00:00.000Z",
           "tweet_count": 0
       },
       {
           "end": "2021-06-01T00:00:00.000Z",
           "start": "2021-05-31T00:00:00.000Z",
           "tweet_count": 0
       }
   ],
   "meta": {
       "total_tweet_count": 22
   }
}
第六步:分页获取结果 如果响应中的 meta 对象包含 next_token,您可以将其值传递给 next_token 查询参数。
KeyValueDescription
next_token将上一条请求的 meta 对象中获取的 next_token 填入此处。如果您最近一次请求未返回全部剩余结果,您会在 meta 对象中收到一个 next_token。提取该字段的值,并在下一次请求中将其作为 next_token 参数的值,同时保持其他请求参数不变。
在正确设置 next_token 的值后,点击“Send”按钮,即可收到下一页的结果。
I