> ## Documentation Index
> Fetch the complete documentation index at: https://docs.x.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 連携ガイド

> このガイドでは、blocks エンドポイントをアプリケーションに連携するために必要な主要概念を説明します。X API v2 スタンダード階層の blocks に関するリファレンスドキュメントです。

export const Button = ({href, children}) => {
  return <div className="not-prose">
    <a href={href}>
      <button className="x-btn">
        <span>{children}</span>
        <svg width="3" height="24" viewBox="0 -9 3 24" class="h-6 rotate-0 overflow-visible"><path d="M0 0L3 3L0 6" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></path></svg>
      </button>
    </a>
  </div>;
};

このガイドでは、blocks エンドポイントをアプリケーションに連携するために必要な主要概念を説明します。

<Callout icon="/icons/xds/icon-key.svg" color="#22C55E" iconType="regular">
  ユーザーのブロックおよびブロック解除エンドポイントは、Enterprise プランでのみ利用可能です。Enterprise の関心表明フォームは[こちら](/forms/enterprise-api-interest)からご記入いただけます。
</Callout>

***

## 認証

Blocks エンドポイントはユーザー認証が必要です:

| Method                                                                                                                         | Description    |
| :----------------------------------------------------------------------------------------------------------------------------- | :------------- |
| [OAuth 2.0 Authorization Code with PKCE](/resources/fundamentals/authentication#oauth-2-0-authorization-code-flow-with-pkce-2) | 新規アプリケーションには推奨 |
| [OAuth 1.0a User Context](/resources/fundamentals/authentication)                                                              | レガシーサポート       |

<Warning>
  App-Only 認証はサポートされていません。必ずユーザーの代理として認証する必要があります。
</Warning>

### 必要なスコープ (OAuth 2.0)

| Scope         | Required for      |
| :------------ | :---------------- |
| `block.read`  | ブロック中のアカウントの取得    |
| `block.write` | アカウントのブロック・ブロック解除 |
| `users.read`  | block 系スコープと併せて必要 |

***

## エンドポイントの概要

| Method | Endpoint                                            | Description       | Availability                                                                                                                                                                    |
| :----- | :-------------------------------------------------- | :---------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| GET    | `/2/users/:id/blocking`                             | ブロック中のアカウントの一覧を取得 | <div className="flex items-center gap-1 flex-nowrap whitespace-nowrap"><Badge color="blue" size="xs">Pay-per-use</Badge><Badge color="green" size="xs">Enterprise</Badge></div> |
| POST   | `/2/users/:id/blocking`                             | アカウントをブロック        | <Badge color="green" size="xs">Enterprise</Badge>                                                                                                                               |
| DELETE | `/2/users/:source_user_id/blocking/:target_user_id` | アカウントのブロックを解除     | <Badge color="green" size="xs">Enterprise</Badge>                                                                                                                               |

***

## Fields と expansions

### デフォルトのレスポンス

```json theme={null}
{
  "data": [
    {
      "id": "1234567890",
      "name": "Example User",
      "username": "example"
    }
  ]
}
```

### 利用可能なフィールド

<Accordion title="user.fields">
  | Field               | Description    |
  | :------------------ | :------------- |
  | `created_at`        | アカウント作成日       |
  | `description`       | ユーザーの bio      |
  | `profile_image_url` | アバターの URL      |
  | `public_metrics`    | フォロワー/フォロー中の件数 |
  | `verified`          | 認証済みステータス      |
</Accordion>

<Accordion title="expansions">
  | Expansion         | Description      |
  | :---------------- | :--------------- |
  | `pinned_tweet_id` | ユーザーのピン留めした Post |
</Accordion>

***

## ブロック時に起こること

<CardGroup cols={2}>
  <Card title="相手ができないこと" icon="xmark">
    * あなたの Post を見ること (ログアウト時を除く)
    * あなたをフォローすること
    * あなたに DM を送ること
    * あなたを List に追加すること
    * 写真にあなたをタグ付けすること
  </Card>

  <Card title="あなたができないこと" icon="xmark">
    * 相手の Post を見ること
    * 相手をフォローすること
    * 相手に DM を送ること
  </Card>
</CardGroup>

<Note>
  あなたをフォローしていた相手をブロックすると、そのフォローは自動的に解除されます。
</Note>

***

## ページネーション

ブロックリストが多いユーザーの場合、結果はページネーションされます:

<CodeGroup dropdown>
  ```bash cURL theme={null}
  # 最初のリクエスト
  curl "https://api.x.com/2/users/123/blocking?max_results=100" \
    -H "Authorization: Bearer $USER_ACCESS_TOKEN"

  # pagination_token を使った後続のリクエスト
  curl "https://api.x.com/2/users/123/blocking?max_results=100&pagination_token=NEXT_TOKEN" \
    -H "Authorization: Bearer $USER_ACCESS_TOKEN"
  ```

  ```python title="Python SDK" lines wrap icon="python" theme={null}
  from xdk import Client

  # OAuth 2.0 ユーザー access token を使用
  client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")

  # SDK はページネーションを自動で処理します
  all_blocked = []

  for page in client.users.get_blocking(user_id="123", max_results=100):
      if page.data:
          all_blocked.extend(page.data)

  print(f"Blocked {len(all_blocked)} users")
  ```

  ```javascript title="JavaScript SDK" lines wrap icon="square-js" theme={null}
  import { Client } from "@xdevplatform/xdk";

  const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });

  async function getAllBlockedUsers(userId) {
    const allBlocked = [];

    // SDK はページネーションを自動で処理します
    const paginator = client.users.getBlocking(userId, { maxResults: 100 });

    for await (const page of paginator) {
      if (page.data) {
        allBlocked.push(...page.data);
      }
    }

    return allBlocked;
  }

  // 使用例
  const blocked = await getAllBlockedUsers("123");
  console.log(`Blocked ${blocked.length} users`);
  ```
</CodeGroup>

***

## エラー処理

| Status | Error             | Solution                                                                 |
| :----- | :---------------- | :----------------------------------------------------------------------- |
| 400    | Invalid request   | user ID の形式を確認                                                           |
| 401    | Unauthorized      | access token を確認                                                         |
| 403    | Forbidden         | スコープと権限を確認。ブロック/ブロック解除は [Enterprise](/forms/enterprise-api-interest) が必要 |
| 404    | Not Found         | ユーザーが存在しない                                                               |
| 429    | Too Many Requests | 待機してから再試行                                                                |

***

## 次のステップ

<CardGroup cols={2}>
  <Card title="クイックスタート" icon="https://mintcdn.com/x-preview/oR-aRNyj1BKPJtxM/icons/xds/icon-rocket.svg?fit=max&auto=format&n=oR-aRNyj1BKPJtxM&q=85&s=b978d7a9225de31709efbbed5b84e92d" href="/x-api/users/blocks/quickstart" width="24" height="24" data-path="icons/xds/icon-rocket.svg">
    最初の blocks リクエストを行う
  </Card>

  <Card title="Mutes" icon="volume-xmark" href="/x-api/users/mutes/introduction">
    ブロックの代わりにユーザーをミュート
  </Card>

  <Card title="API リファレンス" icon="https://mintcdn.com/x-preview/ygI6sSJPehlc0qNT/icons/xds/icon-code.svg?fit=max&auto=format&n=ygI6sSJPehlc0qNT&q=85&s=488e23401b19225b89acc0136d242219" href="/x-api/users/get-blocking" width="24" height="24" data-path="icons/xds/icon-code.svg">
    エンドポイントの詳細ドキュメント
  </Card>

  <Card title="サンプルコード" icon="github" href="https://github.com/xdevplatform/Twitter-API-v2-sample-code">
    動作するコード例
  </Card>
</CardGroup>
