本快速入门指南将帮助你使用 Postman,向全量归档搜索端点发出首个请求,并指定一组 fields。
如果你想查看不同语言的示例代码,请访问我们的 X API v2 sample code GitHub 代码库。
先决条件
全量归档搜索端点目前仅向 Pro 与 企业版 访问开放。要使用此端点,你必须升级到 Pro 访问,或 申请企业版访问级别。除获得访问批准外,你还需要一组用于认证请求的 keys and tokens。你可以按照以下步骤生成这些 keys 和 tokens:
-
在开发者门户中,进入拥有 企业版 或 Pro 访问权限的 Project,并确保该 Project 下关联了一个 developer App。
-
前往你的 应用 的“Keys and tokens”页面以生成所需的凭证。请务必将所有凭证妥善保管。
步骤一:选择工具或库
有多种工具、代码示例和库可用于向此端点发起请求,但这里我们将使用 Postman 来简化流程。
要将 X API v2 的 Postman 集合加载到你的环境中,请点击以下按钮:
在你将 X API v2 集合加载到 Postman 后,前往 Search Posts > Full-archive search request。
步骤二:为请求进行认证
要正确地向 X API 发起请求,你需要验证访问权限。对于该端点,你必须使用 OAuth 2.0 App-Only 方法对请求进行认证。
你必须将密钥和令牌,尤其是 App Access Token(也称 App-only Bearer Token),添加到 Postman。你可以在 Postman 右上角选择名为“X API v2”的环境,并将密钥和令牌填入 “initial value” 和 “current value” 字段(点击环境下拉菜单旁的眼睛图标)。
如果设置正确,请求的 authorization 选项卡会自动引用该变量。
步骤三:创建搜索查询
每个全量归档搜索都需要提供一个搜索查询。本示例中,我们将使用一个匹配 @XDevelopers 账号发布的 Posts 的查询。对此我们使用 from 运算符,并将其设置为 XDevelopers(不区分大小写):
from:XDevelopers
在 Postman 中,前往 “Params” 选项卡,将此查询或以逗号分隔的一串 Post ID 输入到 query 参数的 “Value” 列中。
| Key | Value | Description |
|---|
query | from:XDevelopers | 提交给全量归档搜索端点的搜索查询 |
步骤四:确定并指定希望检索的字段
如果在步骤三后点击 “Send” 按钮,你将在响应中收到默认的 Post object 字段:id、text 和 edit_history_tweet_ids。请注意,如果返回的是编辑功能推出之前的 Posts,将不会提供 edit_history_tweet_ids 字段;该字段未进行历史回填。
如果你希望在这些默认字段之外返回更多字段,需要在请求中通过 field 和/或 expansion 参数指定。
在本练习中,我们将从不同对象请求四组字段:
- 默认的 Post 对象字段
- 主体 Post 对象中的附加 tweet.created_at 字段
- 返回的 Posts 对应作者的 user 对象 默认字段
- 关联用户对象中的附加 user.description 字段
在 Postman 中,前往 “Params” 选项卡,在 “Query Params” 表中添加以下 key:value 对:
| Key | Value | Returned fields |
|---|
tweet.fields | created_at | tweets.created_at |
expansions | author_id | includes.users.id, includes.users.name, includes.users.username |
user.fields | description | includes.users.description |
你现在应该会在 “Send” 按钮旁看到以下 URL:
https://api.x.com/2/tweets/search/all?query=from:XDevelopers&tweet.fields=created_at&expansions=author_id&user.fields=created_at
请注意默认情况下,只会返回最近的 10 条 Posts。如果你希望每个请求返回超过 10 条 Posts,可以使用 max_results 参数,并将其设置为每个请求最多 500 条 Posts。同样,默认会返回最近 30 天内的 Posts。如果你想获取早于 30 天的 Posts,可以在 API 调用中使用 start_time 和 end_time 参数。
步骤五:发起请求并查看响应
完成所有设置后,点击“Send”按钮,您将收到类似以下的响应:
{
"data": [
{
"author_id": "2244994945",
"created_at": "2020-06-11T16:05:06.000Z",
"id": "1271111223220809728",
"text": "Tune in tonight and watch as @jessicagarson takes us through running your favorite Python package in R. 🍿\n\nLearn how to use two powerful programming languages for data science together, and see a live example that uses the recent search endpoint from Twitter’s Developer Labs. https://t.co/v178oUZNuj"
},
{
"author_id": "2244994945",
"created_at": "2020-06-10T19:25:24.000Z",
"id": "1270799243071062016",
"text": "As we work towards building the new Twitter API, we’ve extended the deprecation timeline for several Labs v1 endpoints. Learn more 📖 https://t.co/rRWaJYJgKk"
},
{
"author_id": "2244994945",
"created_at": "2020-06-09T18:08:47.000Z",
"id": "1270417572001976322",
"text": "Annotations help you learn more about a Tweet — they can even help you find topics of interest. 🔬\n\nIn this tutorial, @suhemparack shows us how find Tweets related to COVID-19 using annotations + the filtered stream endpoint.\n\nLearn how you can, too. ⤵️\nhttps://t.co/qwVOgw0zSV"
}
],
"includes": {
"users": [
{
"description": "Twitter #DevRel 团队之声,你获取 Twitter API 更新、新闻与活动的官方来源。\n\n#BlackLivesMatter",
"id": "2244994945",
"name": "Twitter Dev",
"username": "TwitterDev"
}
]
},
"meta": {
"newest_id": "1271111223220809728",
"oldest_id": "1270417572001976322",
"result_count": 3
}
}
在此示例中,我们使用了一个非常简单的查询。若需更详细的指南,请参阅下方资源。
后续步骤
使用 API 参考自定义请求
查看查询运算符完整列表
使用这些端点的示例代码