Documentation Index
Fetch the complete documentation index at: https://generaltranslation.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
このガイドでは、リアルタイムのアカウントアクティビティイベントを受信するためのアクティビティストリームサブスクリプションの設定方法を説明します。
サブスクリプションを作成する
ユーザーのアクティビティイベントをサブスクライブします。curl -X POST "https://api.x.com/2/activity/subscriptions" \
-H "Authorization: Bearer $BEARER_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"user_id": "2244994945",
"event_types": ["tweet_create_events", "favorite_events", "follow_events"]
}'
import requests
bearer_token = "YOUR_BEARER_TOKEN"
url = "https://api.x.com/2/activity/subscriptions"
headers = {
"Authorization": f"Bearer {bearer_token}",
"Content-Type": "application/json"
}
payload = {
"user_id": "2244994945",
"event_types": ["tweet_create_events", "favorite_events", "follow_events"]
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
レスポンス:{
"data": {
"id": "1234567890",
"user_id": "2244994945",
"event_types": ["tweet_create_events", "favorite_events", "follow_events"],
"created_at": "2024-01-15T10:00:00.000Z"
}
}
ストリームに接続する
イベントを受信するために、永続的な接続を開きます。curl "https://api.x.com/2/activity/stream" \
-H "Authorization: Bearer $BEARER_TOKEN"
import requests
bearer_token = "YOUR_BEARER_TOKEN"
url = "https://api.x.com/2/activity/stream"
headers = {"Authorization": f"Bearer {bearer_token}"}
response = requests.get(url, headers=headers, stream=True)
for line in response.iter_lines():
if line:
print(line.decode("utf-8"))
受信イベントを処理する
イベントは JSON オブジェクトとしてストリームされます。{
"for_user_id": "2244994945",
"event_type": "tweet_create_events",
"created_at": "2024-01-15T10:30:00.000Z",
"tweet_create_events": [
{
"id": "1234567890",
"text": "Hello from the stream!",
"author_id": "2244994945"
}
]
}
| イベント | 説明 |
|---|
tweet_create_events | ユーザーが新しいポストを投稿する |
favorite_events | ユーザーがポストにいいねする |
follow_events | ユーザーがほかのアカウントをフォローする、またはフォローされる |
direct_message_events | ユーザーがDMを送信または受信する |
block_events | ユーザーがブロックまたはブロック解除する |
mute_events | ユーザーがミュートまたはミュート解除する |
すべての有効なサブスクリプションを取得します:curl "https://api.x.com/2/activity/subscriptions" \
-H "Authorization: Bearer $BEARER_TOKEN"
サブスクリプションのイベントタイプを変更します:curl -X PUT "https://api.x.com/2/activity/subscriptions/1234567890" \
-H "Authorization: Bearer $BEARER_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"event_types": ["tweet_create_events", "favorite_events"]
}'
サブスクリプションを削除します:curl -X DELETE "https://api.x.com/2/activity/subscriptions/1234567890" \
-H "Authorization: Bearer $BEARER_TOKEN"
Account Activity API
Webhookベースの代替API
APIリファレンス
エンドポイントの詳細なドキュメント