> ## 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)
  * ユーザーアクセストークン (OAuth 1.0a または OAuth 2.0 PKCE)
</Note>

***

<div id="retweet-a-post">
  ## ポストをリツイートする
</div>

<Steps>
  <Step title="自分のユーザーIDを取得する">
    認証済みのユーザーIDが必要です。[ユーザー照会エンドポイント](/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/retweets" \
        -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.retweet(
          user_id="123456789",
          tweet_id="1228393702244134912"
      )

      print(f"Retweeted: {response.data.retweeted}")
      ```

      ```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.retweet("123456789", {
        tweetId: "1228393702244134912",
      });

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

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

***

<div id="undo-a-retweet">
  ## リツイートを取り消す
</div>

リツイートを取り消すには、次を実行します。

<CodeGroup dropdown>
  ```bash cURL theme={null}
  curl -X DELETE "https://api.x.com/2/users/123456789/retweets/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.unretweet(
      user_id="123456789",
      tweet_id="1228393702244134912"
  )

  print(f"Retweeted: {response.data.retweeted}")
  ```

  ```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.unretweet("123456789", "1228393702244134912");

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

**レスポンス:**

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

***

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

<CardGroup cols={2}>
  <Card title="リツイートのルックアップ" icon="retweet" href="/ja/x-api/posts/retweets/quickstart/retweets-lookup">
    ポストをリツイートしたユーザーを取得
  </Card>

  <Card title="引用ポスト" icon="quote-left" href="/ja/x-api/posts/quote-tweets/quickstart">
    引用ポストを取得
  </Card>

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