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

# いいねを管理する

> X API を使用して投稿にいいねを付けたり取り消したりする

export const Button = ({href, children}) => {
  return <div className="not-prose group">
    <a href={href}>
      <button className="flex items-center space-x-2.5 py-1 px-4 bg-primary-dark dark:bg-white text-white dark:text-gray-950 rounded-full group-hover:opacity-[0.9] font-medium">
        <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>;
};

このガイドでは、X API を使用してポストに「いいね」する／「いいね」を取り消す方法を説明します。

<Note>
  **前提条件**

  作業を始める前に、次が必要です:

  * 承認済みの App を含む [開発者アカウント](https://developer.x.com/en/portal/petition/essential/basic-info)
  * User Access Token (OAuth 1.0a または OAuth 2.0 PKCE)
</Note>

***

<div id="like-a-post">
  ## ポストをいいねする
</div>

<Steps>
  <Step title="自分のユーザーIDを取得する">
    認証済みユーザーのIDが必要です。[user lookup endpoint](/ja/x-api/users/lookup/introduction) を使うか、アクセス・トークンから取得できます (数値の部分があなたのユーザーIDです) 。
  </Step>

  <Step title="ポストIDを取得する">
    ポストを表示しているときのURLからポストIDを確認します：

    ```
    https://x.com/XDevelopers/status/1228393702244134912
                                    └── ここがポストIDです
    ```
  </Step>

  <Step title="いいねリクエストを送信する">
    <CodeGroup dropdown>
      ```bash cURL theme={null}
      curl -X POST "https://api.x.com/2/users/123456789/likes" \
        -H "Authorization: Bearer $USER_ACCESS_TOKEN" \
        -H "Content-Type: application/json" \
        -d '{"tweet_id": "1228393702244134912"}'
      ```

      ```python Python SDK theme={null}
      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.posts.like(
          user_id="123456789",
          tweet_id="1228393702244134912"
      )

      print(f"Liked: {response.data.liked}")
      ```

      ```javascript JavaScript SDK theme={null}
      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.posts.like("123456789", {
        tweetId: "1228393702244134912",
      });

      console.log(`Liked: ${response.data?.liked}`);
      ```
    </CodeGroup>
  </Step>

  <Step title="レスポンスを確認する">
    ```json theme={null}
    {
      "data": {
        "liked": true
      }
    }
    ```
  </Step>
</Steps>

***

<div id="unlike-a-post">
  ## ポストのいいねを取り消す
</div>

ポストのいいねを取り消します:

<CodeGroup dropdown>
  ```bash cURL theme={null}
  curl -X DELETE "https://api.x.com/2/users/123456789/likes/1228393702244134912" \
    -H "Authorization: Bearer $USER_ACCESS_TOKEN"
  ```

  ```python Python SDK theme={null}
  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.posts.unlike(
      user_id="123456789",
      tweet_id="1228393702244134912"
  )

  print(f"Liked: {response.data.liked}")
  ```

  ```javascript JavaScript SDK theme={null}
  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.posts.unlike("123456789", "1228393702244134912");

  console.log(`Liked: ${response.data?.liked}`);
  ```
</CodeGroup>

**レスポンス：**

```json theme={null}
{
  "data": {
    "liked": false
  }
}
```

***

<div id="next-steps">
  ## 次のステップ
</div>

<CardGroup cols={2}>
  <Card title="いいねの取得" icon="heart" href="/ja/x-api/posts/likes/quickstart/likes-lookup">
    ポストにいいねしたユーザーを取得
  </Card>

  <Card title="APIリファレンス" icon="code" href="/ja/x-api/posts/like-a-post">
    エンドポイントの詳細なドキュメント
  </Card>
</CardGroup>
