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

# 匿名セッションを設定する

> Auth0 Management API を使用して匿名セッションを設定する方法を説明します。

export const ReleaseStageNotice = ({feature, stage, plans, contact, terms}) => {
  const stageTextMap = {
    "beta": "Beta",
    "ea": "早期アクセス"
  };
  const stageText = stageTextMap[stage] || "製品リリース段階";
  const prsLink = "/docs/troubleshoot/product-lifecycle/product-release-stages";
  const linkify = (text, url) => {
    return <a href={url} target="_blank" rel="noreferrer" class="link">{text}</a>;
  };
  const includeDetails = (plans, contact, terms) => {
    const hasDetails = terms || plans || contact;
    if (!hasDetails) return null;
    return <span data-as="p">
            {plans && <>この機能は{linkify(`${plans}プラン`, "https://auth0.com/pricing")}でご利用いただけます。 </>}
            {contact && "参加をご希望の場合は、" + contact + "までお問い合わせください。 "}
            {terms && <>この機能を使用することにより、Oktaの該当する無料トライアル規約および{linkify("Master Subscription Agreement", "https://www.okta.com/legal")}に同意したものとみなされます。</>}
        </span>;
  };
  return <Warning>
            <span data-as="p">
                <strong>{feature}機能は現在、{linkify(stageText, prsLink)}です。</strong>
            </span>

            {includeDetails(plans, contact, terms)}
        </Warning>;
};

<ReleaseStageNotice feature="匿名セッション" stage="beta" terms="true" contact="Auth0 Support" />

匿名セッションを設定するには、Auth0の[Management API](/docs/ja-jp/api/management/v2)を使用します。

<div id="prerequisites">
  ## 前提条件
</div>

匿名セッションを使用するには、以下が必要です。

* [Auth0 アカウントを作成](https://auth0.com/)します。
* [Auth0 アプリケーションを登録](/docs/ja-jp/get-started/auth0-overview/create-applications)します。Auth0 アプリケーションがない場合は、Auth0 React または Next.js 向けの[Quickstarts](https://auth0.com/docs/quickstart/spa/react)から始めることができます。
* [API (リソースサーバー) を登録](/docs/ja-jp/get-started/auth0-overview/set-up-apis)します。

<div id="auth0-management-api">
  ## Auth0 Management API
</div>

<div id="configure-anonymous-sessions-in-your-auth0-tenant">
  ### Auth0 テナントで匿名セッションを設定する
</div>

匿名セッションの有効期間を設定するには、[`/api/v2/tenants/settings`](/docs/ja-jp/api/management/v2/tenants/patch-settings) エンドポイントに `PATCH` リクエストを送信します。

```json theme={null}
{
  "sessions": {
    "anonymous": {
      "lifetime_in_minutes": 1440
    }
  }
}  
```

<div id="enable-anonymous-sessions-in-your-application">
  ### アプリケーションで匿名セッションを有効にする
</div>

Auth0アプリケーションで匿名セッションを有効にするには、[`/api/v2/clients/{id}`](/docs/ja-jp/api/management/v2/clients/patch-clients-by-id)エンドポイントに`PATCH`リクエストを送ります。

```json theme={null}
{
  "anonymous_sessions": {
    "active": true
  }
}
```

<div id="enable-anonymous-access-in-your-api">
  ### API で匿名アクセスを有効にする
</div>

API で匿名セッションを有効にするには、[`/api/v2/resource-servers/{id}`](/docs/ja-jp/api/management/v2/resource-servers/patch-resource-servers-by-id) エンドポイントに `PATCH` リクエストを送ります。

```json theme={null}
{
  "allow_anonymous_access": true,
  "token_lifetime_for_anonymous_access_tokens": 86400,
  "subject_type_authorization": {
    "user":           { "policy": "allow_all" },
    "client":         { "policy": "deny_all" },
    "anonymous_user": { "policy": "require_client_grant" }
  }
}
```

<div id="create-an-api-access-policy-for-anonymous-users">
  ### 匿名ユーザー用の API Access ポリシーを作成する
</div>

API で匿名セッションを有効にしたら、[`/api/v2/client-grants`](/docs/ja-jp/api/management/v2/client-grants/post-client-grants) エンドポイントに `POST` リクエストを送信します。

```json theme={null}
{
  "audience": "{{anon_sessions_audience_identifier}}",
  "client_id": "{{anon_sessions_clientid}}",
  "scope": [ "read", "write", "delete" ],
  "subject_type": "anonymous_user"
}
```

<div id="create-an-anonymous-session">
  ## 匿名セッションを作成する
</div>

テナント、アプリケーション、API で匿名セッションを設定したら、`/anonymous/token` エンドポイントに `POST` リクエストを送信することで、匿名セッションを作成できます。

```json theme={null}
{
  "client_id": "YOUR_CLIENT_ID",
  "audience": "YOUR_AUDIENCE",
  "scope": "anon"
}
```

レスポンスには、`session_token`と`access_token`が含まれます。

```json theme={null}
{
  "session_token": "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0...",
  "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
  "token_type": "Bearer",
  "expires_in": 86400
}
```

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

* [匿名セッションのユースケース](/docs/ja-jp/manage-users/sessions/anonymous-sessions/anonymous-sessions-use-cases) 匿名セッションのユースケースについて詳しく説明します。
