跳转到主要内容

开始使用全量归档 Post 计数端点

本快速入门指南将帮助你使用 Postman(一个图形化工具,可用于发起 HTTP 请求)向全量归档 Post 计数端点发送首个请求。 如果你想查看不同编程语言的示例代码,请访问我们的 X API v2 sample code GitHub 代码库。

先决条件

全量归档 Post 计数端点目前仅向拥有 Pro 或企业版访问权限的用户开放。要使用此端点,你必须申请 Pro 或企业版访问权限除获批访问外,你还需要一组keys and tokens来对请求进行认证。你可以按以下步骤生成这些 keys 和 tokens:
  • 在开发者门户中进入你拥有企业版访问权限的Project,并确保该 Project 下关联了一个developer App
  • 前往你的应用的“Keys and tokens”页面生成所需凭证。请务必将所有凭证妥善保管。

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

步骤一:选择工具或库 有多种工具、代码示例和库可用于向此端点发起请求,但这里我们将使用 Postman 来简化流程。 要将 X API v2 的 Postman 集合加载到你的环境中,请点击以下按钮: 在 Postman 中加载 X API v2 集合后,前往 Post counts > Full-archive Post counts 请求。 步骤二:为请求进行认证 要正确地向 X API 发起请求,你需要验证访问权限。对于该端点,你必须使用 OAuth 2.0 App-Only 方式为请求进行认证。 你必须将密钥和令牌,特别是 App Access Token(也称 App-only Bearer Token)添加到 Postman。你可以在 Postman 右上角选择名为“X API v2”的环境,并将你的密钥和令牌添加到“initial value”和“current value”字段(点击环境下拉菜单旁的眼睛图标)。 如果操作正确,请求的 authorization 选项卡会自动引用该变量。   步骤三:创建查询 每个全量归档 Post 计数请求都需要一个 query。在本示例中,我们将使用一个匹配 @XDevelopers 账号发布内容的查询。对此查询我们使用 from 运算符,并将其设为 XDevelopers(不区分大小写): from:XDevelopers 在 Postman 中,前往“Params”选项卡,将此查询 ID,或用逗号分隔的一串 Post ID,输入到 ids 参数的“Value”列中。  
KeyValueDescription
queryfrom:XDevelopers提交到全量归档 Post 计数端点的查询
步骤四(可选):指定粒度和时间范围 如果在步骤三之后点击“Send”按钮,你将获得默认的全量归档 Post 计数:过去 30 天内按小时统计。若想按天获取全量归档 Post 计数,需要添加 granularity 参数并将其值设为 day。若想获取超过 30 天之前的 Post 计数,则需指定 start_time 和 end_time 参数及其所需值。  在 Postman 中,前往“Params”选项卡,在“Query Params”表中添加以下键值对:
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”按钮,即可获得下一页结果。