前提条件始める前に、以下が必要です:
- 承認済みの App がある developer account
- App の Bearer Token (ルックアップ用)
- ユーザー Access Token (follows の管理用)
ユーザーのフォロワーを取得
特定ユーザーをフォローしているユーザーのリストを取得します:cURL
curl "https://api.x.com/2/users/2244994945/followers?\
user.fields=username,verified,public_metrics&\
max_results=100" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# ページネーションでユーザーのフォロワーを取得
for page in client.users.get_followers(
"2244994945",
user_fields=["username", "verified", "public_metrics"],
max_results=100
):
for user in page.data:
print(f"{user.username} - Followers: {user.public_metrics.followers_count}")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });
// ページネーションでユーザーのフォロワーを取得
const paginator = client.users.getFollowers("2244994945", {
userFields: ["username", "verified", "public_metrics"],
maxResults: 100,
});
for await (const page of paginator) {
page.data?.forEach((user) => {
console.log(`${user.username} - Followers: ${user.public_metrics?.followers_count}`);
});
}
レスポンス
{
"data": [
{
"id": "1234567890",
"name": "Developer",
"username": "dev_user",
"verified": false,
"public_metrics": {
"followers_count": 500,
"following_count": 200,
"tweet_count": 1500
}
}
],
"meta": {
"result_count": 1,
"next_token": "abc123"
}
}
ユーザーがフォローしているアカウントを取得
特定ユーザーがフォローしているユーザーのリストを取得します:cURL
curl "https://api.x.com/2/users/2244994945/following?\
user.fields=username,verified&\
max_results=100" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# あるユーザーがフォローしているユーザーを取得
for page in client.users.get_following(
"2244994945",
user_fields=["username", "verified"],
max_results=100
):
for user in page.data:
print(f"{user.username} - Verified: {user.verified}")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });
// あるユーザーがフォローしているユーザーを取得
const paginator = client.users.getFollowing("2244994945", {
userFields: ["username", "verified"],
maxResults: 100,
});
for await (const page of paginator) {
page.data?.forEach((user) => {
console.log(`${user.username} - Verified: ${user.verified}`);
});
}
ユーザーをフォロー
認証済みユーザーの代理でユーザーをフォローします:cURL
curl -X POST "https://api.x.com/2/users/123456789/following" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"target_user_id": "2244994945"}'
from xdk import Client
from xdk.oauth1_auth import OAuth1
oauth1 = OAuth1(
api_key="YOUR_API_KEY",
api_secret="YOUR_API_SECRET",
access_token="YOUR_ACCESS_TOKEN",
access_token_secret="YOUR_ACCESS_TOKEN_SECRET"
)
client = Client(auth=oauth1)
# ユーザーをフォロー
response = client.users.follow(
source_user_id="123456789",
target_user_id="2244994945"
)
print(f"Following: {response.data.following}")
import { Client, OAuth1 } from "@xdevplatform/xdk";
const oauth1 = new OAuth1({
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_API_SECRET",
accessToken: "YOUR_ACCESS_TOKEN",
accessTokenSecret: "YOUR_ACCESS_TOKEN_SECRET",
});
const client = new Client({ oauth1 });
// ユーザーをフォロー
const response = await client.users.follow("123456789", {
targetUserId: "2244994945",
});
console.log(`Following: ${response.data?.following}`);
レスポンス
{
"data": {
"following": true,
"pending_follow": false
}
}
対象アカウントが非公開の場合、フォローリクエストが承認されるまで
pending_follow は true になります。ユーザーのフォローを解除
認証済みユーザーの代理でユーザーのフォローを解除します:cURL
curl -X DELETE "https://api.x.com/2/users/123456789/following/2244994945" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN"
from xdk import Client
from xdk.oauth1_auth import OAuth1
oauth1 = OAuth1(
api_key="YOUR_API_KEY",
api_secret="YOUR_API_SECRET",
access_token="YOUR_ACCESS_TOKEN",
access_token_secret="YOUR_ACCESS_TOKEN_SECRET"
)
client = Client(auth=oauth1)
# ユーザーのフォローを解除
response = client.users.unfollow(
source_user_id="123456789",
target_user_id="2244994945"
)
print(f"Following: {response.data.following}")
import { Client, OAuth1 } from "@xdevplatform/xdk";
const oauth1 = new OAuth1({
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_API_SECRET",
accessToken: "YOUR_ACCESS_TOKEN",
accessTokenSecret: "YOUR_ACCESS_TOKEN_SECRET",
});
const client = new Client({ oauth1 });
// ユーザーのフォローを解除
const response = await client.users.unfollow("123456789", "2244994945");
console.log(`Following: ${response.data?.following}`);
レスポンス
{
"data": {
"following": false
}
}
共通パラメータ
| Parameter | Description |
|---|---|
max_results | 1 ページあたりの結果数 (1-1000、デフォルト 100) |
pagination_token | 次のページ用のトークン |
user.fields | 追加のユーザーフィールド |
expansions | 含める関連オブジェクト |
次のステップ
User lookup
ユーザープロフィールをルックアップ
Blocks
ユーザーのブロック・解除
Mutes
ユーザーのミュート・解除
API リファレンス
エンドポイントの詳細ドキュメント