> ## 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.

# トークンの取得

> Token Vault に保存された外部プロバイダーのアクセストークンと Auth0 トークンを交換し、アプリケーションがユーザーに代わって外部 API を呼び出せるようにします。

`POST /oauth/token`

Token Vault を使用すると、アプリケーションは Auth0 トークンを外部プロバイダーのアクセストークンと交換し、ユーザーに代わって外部 API を呼び出せます。この交換は OAuth 2.0 Token Exchange ([RFC 8693](https://datatracker.ietf.org/doc/html/rfc8693)) に基づいており、`urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token` グラントタイプを使用します。

このグラントには、送信する `subject_token` のみが異なる 2 つのバリエーションがあります。

* **リフレッシュトークン交換**: Auth0 リフレッシュトークンを外部プロバイダーのアクセストークンと交換します。アプリケーションが Auth0 リフレッシュトークンを保持している場合に使用します。詳細については、[Token Vault を使用したリフレッシュトークン交換](/docs/ja-jp/secure/call-apis-on-users-behalf/token-vault/refresh-token-exchange-with-token-vault)を参照してください。
* **アクセストークン交換**: Auth0 アクセストークンを外部プロバイダーのアクセストークンと交換します。シングルページアプリケーションなどからバックエンド API が Auth0 アクセストークンのみを受け取る場合に使用します。詳細については、[Token Vault を使用したアクセストークン交換](/docs/ja-jp/secure/call-apis-on-users-behalf/token-vault/access-token-exchange-with-token-vault)を参照してください。

詳細については、[Token Vault のドキュメント](/docs/ja-jp/secure/call-apis-on-users-behalf/token-vault)を参照してください。

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

* 交換を実行する前に、[Token Vault の接続を設定](/docs/ja-jp/secure/call-apis-on-users-behalf/token-vault/configure-token-vault)し、[My Account API](/docs/ja-jp/manage-users/my-account-api)を使用する [Connected Accounts フロー](/docs/ja-jp/secure/call-apis-on-users-behalf/token-vault/connected-accounts-for-token-vault)を通じて、ユーザーの外部アカウントを接続する必要があります。

* **リフレッシュトークン交換**では、`subject_token_type` を `urn:ietf:params:oauth:token-type:refresh_token` に設定します。Token Vault はリフレッシュトークンのローテーションをサポートしていないため、Auth0 Dashboard でアプリケーションの **Allow Refresh Token Rotation** を無効にする必要があります。セキュリティをさらに強化するため、[DPoP](/docs/ja-jp/secure/sender-constraining/demonstrating-proof-of-possession-dpop) を使用して、Auth0 が発行したトークンをクライアントにバインドできます。

* **アクセストークン交換**では、`subject_token_type` を `urn:ietf:params:oauth:token-type:access_token` に設定します。バックエンド API と同じ識別子を持ち、Token Vault のグラントタイプが有効になっている[カスタム API クライアントを作成](/docs/ja-jp/secure/call-apis-on-users-behalf/token-vault/configure-token-vault#create-custom-api-client)する必要があります。バックエンド API は、カスタム API クライアントの資格情報を使用して認証されます。

* 発行されるスコープは、外部アカウントに付与されたスコープと異なる場合があります。この場合、応答 JSON には `scope` フィールドが含まれます。

* Auth0 は、`connection` (および指定されている場合は `login_hint`) に一致するユーザーアイデンティティが見つからない場合、`401` ステータスコードを返します。

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

<ParamField header="DPoP" type="string">
  request に対する DPoP proof。任意ですが、アプリケーションで [Demonstrating Proof-of-Possession](/docs/ja-jp/secure/sender-constraining/demonstrating-proof-of-possession-dpop) を使用する場合にのみ必要です。
</ParamField>

<ParamField header="auth0-forwarded-for" type="string">
  エンドユーザーの IP アドレスを文字列値で指定します。Server-side のシナリオで [不審な IP スロットリング](/docs/ja-jp/secure/attack-protection/suspicious-ip-throttling) 保護を有効にする場合に設定します。
</ParamField>

<div id="request-body">
  ## リクエストボディ
</div>

<ParamField body="grant_type" type="string" required>
  使用するフローを指定します。Token Vault では、`urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token` を使用します。
</ParamField>

<ParamField body="subject_token_type" type="string" required>
  サブジェクトトークンの種類です。リフレッシュトークン交換では、`urn:ietf:params:oauth:token-type:refresh_token` を使用します。アクセストークン交換では、`urn:ietf:params:oauth:token-type:access_token` を使用します。
</ParamField>

<ParamField body="subject_token" type="string" required>
  Auth0 認可サーバーがユーザーを識別するために検証する Auth0 トークンです。リフレッシュトークン交換では Auth0 リフレッシュトークン、アクセストークン交換では Auth0 アクセストークンを指定します。
</ParamField>

<ParamField body="requested_token_type" type="string" required>
  返却を求めるトークンの種類を指定します。Token Vault では、`http://auth0.com/oauth/token-type/federated-connection-access-token` を使用します。
</ParamField>

<ParamField body="connection" type="string" required>
  外部プロバイダーとの接続名です。たとえば、`google-oauth2` です。
</ParamField>

<ParamField body="client_id" type="string" required>
  アプリケーションの Client ID です。アクセストークン交換では、カスタム API クライアントの Client ID です。他のグラントタイプと同様に、HTTP Basic Auth を使用して Authorization ヘッダーで Client ID を渡すこともできます。
</ParamField>

<ParamField body="client_secret" type="string">
  (任意) アプリケーションの Client Secret です。外部プロバイダーのアクセストークンを取得するには、任意のクライアント認証方法を使用できます。他のグラントタイプと同様に、HTTP Basic Auth を使用して Authorization ヘッダーで Client Secret を渡すこともできます。代替方法については、[Auth0 Authentication API リファレンスドキュメント](https://auth0.com/docs/api/authentication#authentication-methods)を参照してください。
</ParamField>

<ParamField body="login_hint" type="string">
  (任意) `login_hint` は、職場用と個人用の Google アカウントなど、ユーザーが同じ接続に複数のアカウントを持つ場合にのみ使用してください。トークン交換時に `login_hint` に値を渡すと、リクエストの対象となるユーザーのリンク済みアカウントを明示的に指定できます。
</ParamField>

<div id="response">
  ## レスポンス
</div>

| ステータス | 説明                                                                                              |
| ----- | ----------------------------------------------------------------------------------------------- |
| 200   | 成功時のレスポンス。外部プロバイダーのアクセストークン、スコープ、有効期限を返します。                                                     |
| 401   | 未認可。サブジェクトトークンが無効または期限切れであるか、クライアント資格情報が正しくないか、Auth0 が `connection` に一致するユーザーアイデンティティを見つけられません。 |
| 403   | 禁止。クライアントにはトークン交換を実行する権限がありません。                                                                 |

<ResponseExample>
  ```json 200 Response theme={null}
  {
    "access_token": "<YOUR_GOOGLE_ACCESS_TOKEN>",
    "scope": "https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid",
    "expires_in": 1377,
    "issued_token_type": "http://auth0.com/oauth/token-type/federated-connection-access-token",
    "token_type": "Bearer"
  }
  ```
</ResponseExample>

<div id="response-fields">
  ## レスポンスフィールド
</div>

<ResponseField name="access_token" type="string">
  外部プロバイダーのアクセストークンです。このトークンを使用して、ユーザーに代わって外部プロバイダーのAPIを呼び出します。
</ResponseField>

<ResponseField name="scope" type="string">
  外部プロバイダーのアクセストークンに付与されたスコープのスペース区切りリストです。
</ResponseField>

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

<ResponseField name="issued_token_type" type="string">
  返されるトークンの形式を示します。

  値: `http://auth0.com/oauth/token-type/federated-connection-access-token`
</ResponseField>

<ResponseField name="token_type" type="string">
  Authorizationヘッダーで使用する認証スキームを指定します。Token Vaultでは`Bearer`です。
</ResponseField>
