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

# トークンを取得

> Custom Token Exchange grant を使用して、既存のIDトークンをAuth0トークンに交換します。

export const ResponseSchema = ({statusCode, type = "{}", children}) => {
  const [open, setOpen] = useState(false);
  return <div className="border border-gray-100 dark:border-gray-800 rounded-lg mb-3 overflow-hidden">
      <div className={`flex items-center gap-2.5 px-4 py-2.5 cursor-pointer select-none ${open ? "bg-gray-50 dark:bg-gray-800" : ""}`} onClick={() => setOpen(!open)}>
        {statusCode && <span className="border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 font-mono text-xs px-1.5 py-0.5 rounded">
            {statusCode.startsWith("default") ? "default" : statusCode}
          </span>}
        <span className="text-gray-500 dark:text-gray-400 text-sm font-mono">
          {type}
        </span>
        <span className="text-gray-400 dark:text-gray-500 text-sm italic">
          application/json
        </span>
        <svg className={`ml-auto opacity-50 transition-transform duration-200 ${open ? "rotate-180" : ""}`} width="16" height="16" viewBox="0 0 16 16" fill="none">
          <path d="M4 6l4 4 4-4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
        </svg>
      </div>
      {open && <div className="px-4 pt-1 pb-3 border-t border-gray-100 dark:border-gray-800">
          {children}
        </div>}
    </div>;
};

<div id="endpoint">
  ## エンドポイント
</div>

`POST /oauth/token`

Custom Token Exchange (CTE) は、アプリケーションが `/oauth/token` エンドポイント を呼び出して、既存のIDトークンをAuth0トークンに交換するための仕組みであり、[RFC 8693](https://datatracker.ietf.org/doc/html/rfc8693) の仕様に準拠しています。この機能は、同じユーザーに代わって別のaudienceへアクセスするために既存のAuth0トークンを交換する場合や、外部IDプロバイダーとの統合、Auth0プラットフォームへのシームレスなユーザー移行など、高度な統合要件に対応するうえで重要です。交換プロセスは完全に管理可能で、開発者は関連するユースケース専用のAuth0 Actionで実行されるカスタムロジックを使用して、その詳細を制御できます。

<Note>
  Custom Token Exchange は現在Early Accessとして提供されています。この機能を使用することで、[Okta’s Master Subscription Agreement](https://www.okta.com/legal/) に定める該当のFree Trial条項に同意したものとみなされます。ユーザーの `subject_token` を安全に検証する責任はお客様にあります。詳細は [User Guide](https://auth0.com/docs/authenticate/custom-token-exchange) を参照してください。
</Note>

<div id="remarks">
  ### 注記
</div>

[カスタムトークン交換](/docs/ja-jp/authenticate/custom-token-exchange)を参照して、カスタムトークン交換プロファイル、プロファイルとActionの対応付け、このフローをアプリケーションで有効にする方法をご確認ください。

* アプリケーションに発行されるスコープは、要求したスコープと異なる場合があります。その場合、レスポンスJSONにはスコープパラメータが含まれます。スコープは、[アプリケーション向けAPIアクセスポリシー](/docs/ja-jp/get-started/apis/api-access-policies-for-applications)で説明されているとおりにフィルタリングされます。

* MFAは、`setActor()`でactorが設定されたトランザクションには対応していません。その場合、リクエストは次の`400`エラーを返します。`MFA is not supported using actor_token with the requested token exchange profile.`

* Actionが`setActor()`を呼び出すと、発行されるアクセストークンとID トークンには、[委譲チェーン](/docs/ja-jp/secure/call-apis-on-users-behalf/on-behalf-of-token-exchange#the-act-claim)を表す`act`クレームが含まれます。`act`クレームは`userinfo`レスポンスにも含まれます。

* アクセストークンではなくSession Transfer Tokenを要求するには、`audience`を`urn:YOUR_AUTH0_TENANT_DOMAIN:session_transfer`に設定します。このリクエストでは`setActor()`の呼び出しが必要です。生成されたトークンを使用してsubjectユーザーに代わってセッションを確立する方法については、[セッション委譲](/docs/ja-jp/authenticate/single-sign-on/session-delegation)を参照してください。

* 発行されるトークンに標準クレーム (`email`など) を含めるには、Actionでクレームを明示的に設定している場合でも、対応するスコープ (`email`) をリクエストに含める必要があります。詳細については、[OpenID Connect スコープ: 標準クレーム](/docs/ja-jp/get-started/apis/scopes/openid-connect-scopes#standard-claims)を参照してください。

* カスタムトークン交換ではagent actorは追加されません。Agents as Principalが有効な場合でも、`act`クレームは`setActor()`を介してActionが完全に制御します。Auth0がこのグラントに対してagent `act`を自動的に追加することはありません。

<div id="headers">
  ## ヘッダー
</div>

<ParamField header="DPoP" type="string">
  リクエストの DPoP プルーフです。これは任意で、アプリケーションで Demonstrating Proof-of-Possession を使用している場合にのみ必要です。
</ParamField>

<ParamField header="auth0-forwarded-for" type="string">
  エンドユーザーの IP を文字列で指定します。サーバーサイドのシナリオで Suspicious IP Throttling 保護を有効にしたい場合は、これを設定してください。
</ParamField>

<div id="body-parameters">
  ## Body Parameters
</div>

<div className="prose-sm prose-gray dark:prose-invert">
  <span data-as="p">リクエスト本文は `application/x-www-form-urlencoded` 形式です。</span>
</div>

<ParamField body="grant_type" type="string" required>
  使用するフローを示します。カスタムトークン交換 の場合は、`urn:ietf:params:oauth:grant-type:token-exchange` を使用します。

  使用可能な値: `urn:ietf:params:oauth:grant-type:token-exchange`
</ParamField>

<ParamField body="subject_token_type" type="string" required>
  subject token の種類です。カスタムトークン交換 では、`http://acme.com/legacy-token` や `urn:acme:legacy-token` のように、自身が所有する名前空間配下の任意の URI を使用できます。次の名前空間は予約されているため、使用できません: `http://auth0.com`, `https://auth0.com`, `http://okta.com`, `https://okta.com`, `urn:ietf`, `urn:auth0`, `urn:okta`.
</ParamField>

<ParamField body="subject_token" type="string" required>
  subject token です。このトークンは、action で検証し、ユーザーの識別に使用する必要があります。
</ParamField>

<ParamField body="client_id" type="string" required>
  アプリケーションの Client ID です。他の grant type と同様に、HTTP Basic Auth を使用して Authorization ヘッダーで client ID を渡すこともできます。
</ParamField>

<ParamField body="client_secret" type="string">
  (任意) アプリケーションの Client Secret です。他の grant type と同様に、HTTP Basic Auth を使用して Authorization ヘッダーで client secret を渡すこともできます。また、[Auth0 Authentication API reference docs](https://auth0.com/docs/api/authentication#authentication-methods) で説明されているとおり、ほかの方法も利用できます。カスタムトークン交換 はパブリックなアプリケーションでも使用できるため、追加のセキュリティ対策の実装については [Attack Protection](https://auth0.com/docs/authenticate/custom-token-exchange/cte-attack-protection) を参照してください。
</ParamField>

<ParamField body="audience" type="string">
  (任意) アクセス先の対象 API を一意に識別する子です。指定しない場合は、[Tenant Settings](https://auth0.com/docs/get-started/tenant-settings) で設定されたデフォルトのテナント audience が使用されます。アクセストークンの代わりに、委任されたセッションを確立するための [Session Transfer Token](/docs/ja-jp/authenticate/single-sign-on/session-delegation) をリクエストするには、`urn:YOUR_AUTH0_TENANT_DOMAIN:session_transfer` を使用します。
</ParamField>

<ParamField body="resource" type="string">
  (任意) アクセス先の対象 API (resource server) の識別子です。Auth0 テナントに登録されている API Identifier と一致している必要があります。テナントの [Resource Parameter Compatibility Profile](https://auth0.com/docs/get-started/tenant-settings#settings-advanced) が `compatibility` に設定されている場合、`audience` の代わりに使用されます。
</ParamField>

<ParamField body="scope" type="string">
  (任意) OAuth2 のスコープパラメータです。
</ParamField>

<ParamField body="organization" type="string">
  (任意) リクエストに関連付ける organization またはその識別子です。あるいは、[Use Organization Names in Authentication API](https://auth0.com/docs/manage-users/organizations/configure-organizations/use-org-name-authentication-api) が設定されている場合は、organization 名を指定することもできます。
</ParamField>

<ParamField body="actor_token" type="string">
  (任意) subject user に代わって委譲を行う actor を識別するトークンです。`actor_token_type` とあわせて指定する必要があります。指定した場合、リフレッシュトークン は発行されません。
</ParamField>

<ParamField body="actor_token_type" type="string">
  (任意) actor token の種類です。`actor_token` とあわせて指定する必要があります。Auth0 ID トークン の場合は、サーバー側での自動検証 (署名、有効期限、issuer、ユーザー検索) のために `urn:ietf:params:oauth:token-type:id_token` を使用します。それ以外の値については、`subject_token_type` と同じ名前空間の制限に従ってください。
</ParamField>

<div id="response-schema">
  ## レスポンススキーマ
</div>

<ResponseSchema>
  <ResponseField name="access_token" type="string">
    アクセストークン。
  </ResponseField>

  <ResponseField name="refresh_token" type="string">
    リフレッシュトークン。
  </ResponseField>

  <ResponseField name="id_token" type="string">
    ID トークン。
  </ResponseField>

  <ResponseField name="token_type" type="string">
    トークンタイプ。
  </ResponseField>

  <ResponseField name="issued_token_type" type="string">
    発行されたトークンタイプ。標準的な交換では、これは `urn:ietf:params:oauth:token-type:access_token` です。Session Transfer Token を要求する場合は、`urn:auth0:params:oauth:token-type:session_transfer_token` となり、トークン自体は `access_token` で返されます。
  </ResponseField>

  <ResponseField name="expires_in" type="integer">
    アクセストークンの有効期間 (秒) 。
  </ResponseField>
</ResponseSchema>

<div id="response-messages">
  ## レスポンスメッセージ
</div>

| ステータス | 説明                                                                                                                                                             |
| ----- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 200   | 正常に応答しました                                                                                                                                                      |
| 400   | 不正なリクエスト。必須パラメータが不足しているか整合性が取れていない場合 (たとえば、`actor_token`/`actor_token_type` のいずれか一方しか存在しない場合) 、またはセッション転送トークンをリクエストする際に `setActor()` が呼び出されなかった場合に発生することがあります。 |
