メインコンテンツへスキップ

Quote Tweets ルックアップエンドポイントはじめに

このクイックスタートガイドでは、Postman を使って、指定した一連の fields を含めた Quote Tweets ルックアップエンドポイントへの最初のリクエストを送信する手順を説明します。 さまざまな言語のサンプルコードをご覧になりたい場合は、GitHub の X API v2 sample code リポジトリをご参照ください。 

前提条件

このガイドを完了するには、リクエストを認証するためのキーとトークンのセットが必要です。これらのキーとトークンは、以下の手順で生成できます: 引用ポスト検索リクエストの構築手順 ステップ1:ツールまたはライブラリを選択する このエンドポイントへのリクエストを行うために使用できるツール、コード例、ライブラリはいくつかありますが、ここではプロセスを簡素化するためにPostmanツールを使用します。 X API v2 Postmanコレクションを環境に読み込むには、以下のボタンをクリックしてください: PostmanにX API v2コレクションが読み込まれたら、timelineフォルダに移動し、「Quote Tweets by ID」リクエストを見つけてください。 ステップ2:リクエストを認証する X APIに適切にリクエストを行うには、権限があることを確認する必要があります。このエンドポイントでは、OAuth 2.0 アプリ専用OAuth 2.0 認可コード(PKCE 対応)、またはOAuth 1.0a ユーザーコンテキスト認証方式のいずれかでリクエストを認証する必要があります。 簡単にするために、このリクエストではOAuth 2.0 アプリ専用を使用しますが、非公開のメトリクスやポストをリクエストする場合は、他の認証方式のいずれかを使用する必要があります。 キーとトークン、特にアプリアクセストークン(アプリ専用ベアラートークンとも呼ばれます)をPostmanに追加する必要があります。これは、Postmanの右上隅にある「X API v2」という名前の環境を選択し、キーとトークンを「initial value」と「current value」フィールドに追加することで行えます(環境ドロップダウンの横にある目のアイコンをクリックします)。 正しく設定すると、この変数はリクエストの認証タブに自動的に反映されます。 ステップ3:引用ポストを取得したいポストを特定して指定する ポストIDを渡すことで、引用ポストを取得したいポストをリクエスト内で指定する必要があります。 この例では、使用するポストIDは1409931481552543749です。 Postmanで「Params」タブに移動し、このポストIDをidパラメータの「Value」列に入力してください。
KeyValue
id1409931481552543749
ステップ4:取得したいfieldsを特定して指定する ステップ3の後に「Send」ボタンをクリックすると、レスポンスにデフォルトのポストオブジェクトfieldsが返されます:id、text、edit_history_tweet_ids。 追加のfieldsを受け取りたい場合は、fieldおよび/またはexpansionパラメータを使用して、リクエストでそれらのfieldsを指定する必要があります。 この演習では、異なるオブジェクトから3つの異なるfieldsセットをリクエストします:
  1. プライマリユーザーオブジェクトの追加tweet.created_at field
  2. 返されたポストに関連する作成者のユーザーオブジェクトのデフォルトfields:id、name、username
  3. 関連するユーザーオブジェクトの追加user.created_at field
Postmanで「Params」タブに移動し、以下のキー:値ペアを「Query Params」テーブルに追加してください:
KeyValueReturned fields
tweet.fieldscreated_attweets.created_at
expansionsauthor_idincludes.users.id, includes.users.name, includes.users.username
user.fieldscreated_atincludes.users.created_at
次に、「Send」ボタンの横に、次のURLが表示されているはずです: https://api.x.com/2/tweets/:id/quote_tweets?expansions=author_id&tweet.fields=created_at&user.fields=created_at 注意: Postman では、URL フィールド内のパスパラメータ :id は、id パラメータフィールドに入力した値に自動的に更新されません。したがって、上記のURLには 1409931481552543749 ではなく :id が含まれています。 ステップ5: リクエストを送信してレスポンスを確認する すべての設定が完了したら、「Send」ボタンをクリックすると、次のレスポンスが返ってきます:
{
 "data": [
   {
     "created_at": "2022-02-22T04:31:34.000Z",
     "text": "RT @chris_bail: Twitter has created an entire course (with videos, code, and other materials) to help researchers learn how to collect data…",
     "author_id": "29757971",
     "id": "1495979553889697792"
   },
   {
     "created_at": "2022-01-26T17:07:43.000Z",
     "text": "RT @suhemparack: Super excited to share our course on Getting started with the #TwitterAPI v2 for academic research\n\nIf you know students w…",
     "author_id": "241588187",
     "id": "1486385372401737728"
   },
   {
     "created_at": "2022-01-11T17:28:04.000Z",
     "text": "RT @suhemparack: Super excited to share our course on Getting started with the #TwitterAPI v2 for academic research\n\nIf you know students w…",
     "author_id": "24961055",
     "id": "1480954678447857669"
   },
   {
     "created_at": "2022-01-10T20:34:46.000Z",
     "text": "RT @suhemparack: Super excited to share our course on Getting started with the #TwitterAPI v2 for academic research\n\nIf you know students w…",
     "author_id": "1441574419789324291",
     "id": "1480639272721940486"
   },
   {
     "created_at": "2021-12-16T22:55:24.000Z",
     "text": "RT @chris_bail: Twitter has created an entire course (with videos, code, and other materials) to help researchers learn how to collect data…",
     "author_id": "1623598771",
     "id": "1471614967207976961"
   },
   {
     "created_at": "2021-12-13T15:59:55.000Z",
     "text": "RT @suhemparack: Super excited to share our course on Getting started with the #TwitterAPI v2 for academic research\n\nIf you know students w…",
     "author_id": "1506401233",
     "id": "1470423243513372679"
   },
   {
     "created_at": "2021-12-10T02:02:45.000Z",
     "text": "RT @suhemparack: Super excited to share our course on Getting started with the #TwitterAPI v2 for academic research\n\nIf you know students w…",
     "author_id": "40103034",
     "id": "1469125403373568001"
   },
   {
     "created_at": "2021-12-08T17:27:54.000Z",
     "text": "RT @suhemparack: Super excited to share our course on Getting started with the #TwitterAPI v2 for academic research\n\nIf you know students w…",
     "author_id": "1436400156006567936",
     "id": "1468633446935318529"
   },
   {
     "created_at": "2021-09-15T21:40:24.000Z",
     "text": "RT @suhemparack: Super excited to share our course on Getting started with the #TwitterAPI v2 for academic research\n\nIf you know students w…",
     "author_id": "81650379",
     "id": "1438256410417143809"
   },
   {
     "created_at": "2021-08-26T16:45:16.000Z",
     "text": "RT @suhemparack: Super excited to share our course on Getting started with the #TwitterAPI v2 for academic research\n\nIf you know students w…",
     "author_id": "40462535",
     "id": "1430934381829492746"
   }
 ],
 "includes": {
   "users": [
     {
       "username": "j_a_tucker",
       "id": "29757971",
       "name": "Joshua Tucker",
       "created_at": "2009-04-08T16:45:38.000Z"
     },
     {
       "username": "whimchic",
       "id": "241588187",
       "name": "whimchic",
       "created_at": "2011-01-22T16:51:43.000Z"
     },
     {
       "username": "mattbiehl",
       "id": "24961055",
       "name": "Matthias Biehl",
       "created_at": "2009-03-17T21:41:27.000Z"
     },
     {
       "username": "weixinac",
       "id": "1441574419789324291",
       "name": "J",
       "created_at": "2021-09-25T01:25:19.000Z"
     },
     {
       "username": "RSangeleer",
       "id": "1623598771",
       "name": "Richard Sangeleer",
       "created_at": "2013-07-26T18:25:45.000Z"
     },
     {
       "username": "Gulnerman",
       "id": "1506401233",
       "name": "Giz Gulnerman",
       "created_at": "2013-06-11T20:13:40.000Z"
     },
     {
       "username": "efishman123",
       "id": "40103034",
       "name": "Elishema Fishman",
       "created_at": "2009-05-14T22:25:58.000Z"
     },
     {
       "username": "dtcxwz",
       "id": "1436400156006567936",
       "name": "Hüseyin Ateş",
       "created_at": "2021-09-10T18:44:30.000Z"
     },
     {
       "username": "brendaberkelaar",
       "id": "81650379",
       "name": "Dr. Brenda Berkelaar",
       "created_at": "2009-10-11T18:09:16.000Z"
     },
     {
       "username": "misoca",
       "id": "40462535",
       "name": "Michael Soto",
       "created_at": "2009-05-16T13:26:05.000Z"
     }
   ]
 },
 "meta": {
   "result_count": 10,
   "next_token": "avdjwk0udyx6"
 }
}

ステップ6:結果をページングする

前のレスポンスの末尾には、以下の fields を含む meta data オブジェクトがあります。
  • results_count
  • next_token
ステップ4では max_results パラメータを指定していないため、デフォルトで取得できるのは Post が10件のみです。追加のページの data にアクセスするには、最後の結果に含まれる next_token フィールドの値を取得し、その文字列を Postman の params ページで pagination_token パラメータの値として設定し、その他はそのままにします。 
KeyValue
pagination_tokenavdjwk0udyx6
設定が完了したら、「Send」を再度クリックすると、次のページの結果を受け取れるはずです。  この概念についてさらに詳しくは、pagination に関するガイドをご覧ください。