ユーザーのメンションを取得する
1
ユーザー ID を取得する
ユーザールックアップエンドポイントでユーザー ID を取得します。たとえば、@XDevelopers のユーザー ID は
2244994945 です。2
メンションタイムラインをリクエストする
cURL
curl "https://api.x.com/2/users/2244994945/mentions?\
tweet.fields=created_at,public_metrics,author_id&\
expansions=author_id&\
user.fields=username,verified&\
max_results=10" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# ページネーション付きでメンションタイムラインを取得
for page in client.posts.get_user_mentions(
"2244994945",
tweet_fields=["created_at", "public_metrics", "author_id"],
expansions=["author_id"],
user_fields=["username", "verified"],
max_results=10
):
for post in page.data:
print(f"@{post.author_id}: {post.text[:50]}...")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });
// ページネーション付きでメンションタイムラインを取得
const paginator = client.posts.getUserMentions("2244994945", {
tweetFields: ["created_at", "public_metrics", "author_id"],
expansions: ["author_id"],
userFields: ["username", "verified"],
maxResults: 10,
});
for await (const page of paginator) {
page.data?.forEach((post) => {
console.log(`@${post.author_id}: ${post.text?.slice(0, 50)}...`);
});
}
3
レスポンスを確認する
{
"data": [
{
"id": "1301573587187331074",
"text": "Hey @XDevelopers, love the new API!",
"author_id": "1234567890",
"created_at": "2024-01-15T10:30:00.000Z",
"public_metrics": {
"retweet_count": 5,
"reply_count": 2,
"like_count": 42,
"quote_count": 1
}
}
],
"includes": {
"users": [
{
"id": "1234567890",
"username": "developer",
"name": "Dev Person",
"verified": false
}
]
},
"meta": {
"newest_id": "1301573587187331074",
"oldest_id": "1301573587187331074",
"result_count": 1,
"next_token": "t3buvdr5pujq9g7bggsnf3ep2ha28"
}
}
メンションをフィルターする
返信を除外する
ユーザーをメンションするオリジナル投稿のみを取得します:cURL
curl "https://api.x.com/2/users/2244994945/mentions?\
exclude=replies&\
max_results=10" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# 返信を除外してメンションを取得
for page in client.posts.get_user_mentions(
"2244994945",
exclude=["replies"],
max_results=10
):
for post in page.data:
print(post.text)
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });
// 返信を除外してメンションを取得
const paginator = client.posts.getUserMentions("2244994945", {
exclude: ["replies"],
maxResults: 10,
});
for await (const page of paginator) {
page.data?.forEach((post) => {
console.log(post.text);
});
}
期間内のメンションを取得する
cURL
curl "https://api.x.com/2/users/2244994945/mentions?\
start_time=2024-01-01T00%3A00%3A00Z&\
end_time=2024-01-31T23%3A59%3A59Z" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# 期間内のメンションを取得
for page in client.posts.get_user_mentions(
"2244994945",
start_time="2024-01-01T00:00:00Z",
end_time="2024-01-31T23:59:59Z"
):
for post in page.data:
print(f"{post.created_at}: {post.text[:50]}...")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });
// 期間内のメンションを取得
const paginator = client.posts.getUserMentions("2244994945", {
startTime: "2024-01-01T00:00:00Z",
endTime: "2024-01-31T23:59:59Z",
});
for await (const page of paginator) {
page.data?.forEach((post) => {
console.log(`${post.created_at}: ${post.text?.slice(0, 50)}...`);
});
}
一般的なパラメーター
| Parameter | Description | Default |
|---|---|---|
max_results | ページあたりの結果数 (1-100) | 10 |
start_time | 最も古い投稿タイムスタンプ (ISO 8601) | — |
end_time | 最も新しい投稿タイムスタンプ (ISO 8601) | — |
since_id | この ID より後の投稿を返す | — |
until_id | この ID より前の投稿を返す | — |
exclude | retweets、replies、または両方を除外 | — |
pagination_token | 次ページ用のトークン | — |
次のステップ
Home timeline
ユーザーのホームタイムラインを取得
統合ガイド
主要な概念とベストプラクティス
API リファレンス
エンドポイントの完全なドキュメント
ページネーションガイド
大規模な結果セットを移動