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

# Android で My Account API を使用してセルフサービスのアカウントセキュリティ UI を構築する

> Android で Auth0 の My Account API と Universal Components を使用して、セルフサービスのアカウントセキュリティ UI を構築する方法を説明します。

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="Auth0 Universal Components" stage="beta" terms="true" contact="Auth0 Support" />

Android 向け Auth0 Universal Components を使用すると、[Native Android アプリケーション](/docs/ja-jp/get-started/auth0-overview/create-applications#create-application-in-auth0)内に、セルフサービスで利用できるアカウントセキュリティ UI を構築できます。

`AuthenticatorSettingsComponent` を使用すると、ユーザーは Web ブラウザーに移動したりサポートに問い合わせたりすることなく、アプリケーション内で多要素認証 (MFA) の認証要素、パスキー、リカバリーコードなど、自身の認証方法を直接管理できます。

<div id="how-it-works">
  ## 仕組み
</div>

`AuthenticatorSettingsComponent` は、Auth0 の [My Account API の認証方法](/docs/ja-jp/manage-users/my-account-api#manage-authentication-methods)を使用して、アプリケーション内に認証方法管理用の UI をレンダリングします。

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  My Account API には現在、特に Free ティアの tenant において低いレート制限が設定されています。そのため、これらのコンポーネントの使用中にエラーが発生する場合があります。
</Callout>

認証済みのユーザーがアカウント設定画面を開くと、[Auth0.Android](https://github.com/auth0/Auth0.Android) SDK は My Account API の audience を対象とするスコープが設定された[アクセストークン](/docs/ja-jp/secure/tokens/access-tokens)を取得します。

`AuthenticatorSettingsComponent` は アクセストークン を使用して、ログイン中のユーザーとして My Account API の [`/me/v1/authentication-methods`](/docs/ja-jp/api/myaccount/authentication-methods/get-authentication-methods) endpoint を呼び出します。そのため、各ユーザーが変更できるのは自分自身の認証方法のみです。

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  * `AuthenticatorSettingsComponent` は、**エンドユーザー向けセルフサービス**インターフェースを作成します。エンドユーザーは、メール OTP、SMS OTP、TOTP (認証器アプリ) 、Auth0 Guardian を介したプッシュ、パスキー、リカバリーコードなど、アカウントに登録されているすべての認証方法を登録、一覧表示、削除できます。

  * ユーザーが Auth0 Organization を管理する**delegated admin**インターフェースについては、[Build a Delegated Admin Interface](/docs/ja-jp/get-started/universal-components/web/components/build-delegated-admin) を参照してください。
</Callout>

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

<div id="enable-the-my-account-api">
  ### My Account API を有効化する
</div>

1. [Auth0 Dashboard > アプリケーション > APIs](https://manage.auth0.com/#/apis) に移動します。
2. **Activate My Account API** を選択して、テナントで有効化されていることを確認します。

<div id="create-an-application-and-configure-my-account-api-permissions">
  ### アプリケーションを作成し、My Account API の権限を設定する
</div>

1. [Auth0 Dashboard > Applications](https://manage.auth0.com/#/applications) に移動します。

2. **Create Application** を選択します。

3. **Native** を選択します。

4. **Settings** タブを選択し、次のコールバック URL を **Allowed Callback URLs** に追加します。

   ```text theme={null}
   https://YOUR_AUTH0_DOMAIN/android/YOUR_PACKAGE_NAME/callback, YOUR_SCHEME://YOUR_AUTH0_DOMAIN/android/YOUR_PACKAGE_NAME/callback
   ```

5. **Allowed Logout URLs** にも同じ URL を追加します。

   ```text theme={null}
   https://YOUR_AUTH0_DOMAIN/android/YOUR_PACKAGE_NAME/callback, YOUR_SCHEME://YOUR_AUTH0_DOMAIN/android/YOUR_PACKAGE_NAME/callback
   ```

6. **API Access** タブを選択します。

7. **Auth0 My Account API** の **Edit** を選択し、**User-delegated Access** の権限を追加します。

   `create:me:authentication_methods`
   `read:me:authentication_methods`
   `update:me:authentication_methods`
   `delete:me:authentication_methods`

8. **Save** を選択して、権限を保存します。

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  * ユーザーのアクセストークンには、ログイン時に付与された権限のみが含まれます。
  * ユーザーが認証方法を登録、確認、削除できるようにするには、4 つすべてのスコープをリクエストします。
</Callout>

<div id="install-the-sdk">
  ### SDK をインストールする
</div>

Gradle を使用して、`com.auth0.universalcomponents:universal-components` パッケージをインストールします。インストール手順の詳細とプラットフォーム要件については、Auth0 [Universal Components for Android](/docs/ja-jp/get-started/universal-components/android/android-overview) を参照してください。

<div id="initialize-the-sdk">
  ### SDK を初期化する
</div>

SDK を初期化するには、通常、アプリケーションの起動時に `Application` サブクラスまたはランチャー `Activity` の `onCreate` から、`Auth0UniversalComponents.initialize(...)` メソッドを一度だけ呼び出します。

```kotlin MainActivity.kt wrap lines theme={null}
import com.auth0.android.Auth0
import com.auth0.android.authentication.AuthenticationAPIClient
import com.auth0.android.authentication.storage.CredentialsManager
import com.auth0.android.authentication.storage.SharedPreferencesStorage
import com.auth0.universalcomponents.Auth0UniversalComponents
import com.auth0.universalcomponents.token.DefaultTokenProvider

class MainActivity : ComponentActivity() {

    private val account by lazy {
        Auth0.getInstance(
            getString(R.string.com_auth0_client_id),
            getString(R.string.com_auth0_domain)
        )
    }

    private val credentialsManager by lazy {
        CredentialsManager(
            AuthenticationAPIClient(account),
            SharedPreferencesStorage(this)
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Auth0UniversalComponents.initialize(
            context = applicationContext,
            account = account,
            tokenProvider = DefaultTokenProvider(credentialsManager),
            scheme = getString(R.string.com_auth0_scheme),
            passkeyConfiguration = PasskeyConfiguration()
        )

        setContent { MyApp() }
    }
}
```

<div id="configure-the-token-provider">
  ### トークンプロバイダーを設定する
</div>

[`Auth0.Android`](https://github.com/auth0/Auth0.Android) の `CredentialsManager` をラップする `DefaultTokenProvider` を使用して、アプリケーションから資格情報をリクエストします。

<Callout icon="file-lines" color="#0EA5E9" iconType="regular">
  Auth0 では、本番環境での連携には Auth0.Android の `CredentialsManager` を使用することを推奨しています。ストレージ要件を満たせない場合に限り、カスタム `TokenProvider` を実装してください。
</Callout>

Auth0.Android SDK の `CredentialsManager` を使用せずに資格情報を管理するには、インターフェースを直接実装します。

```kotlin wrap lines theme={null}
class AppTokenProvider : TokenProvider {
    override suspend fun fetchCredentials(): Credentials {
        // ユーザーのログイン資格情報を返します。
    }
    override suspend fun fetchApiCredentials(
        audience: String,
        scope: String?
    ): APICredentials {
        // キャッシュされた My Account API の資格情報を返します。期限切れの場合は更新します。
    }
    override suspend fun saveApiCredentials(
        audience: String,
        credentials: APICredentials
    ) {
        // 発行されたばかりの API 資格情報を保存します。
    }
}
```

<Check>
  コンポーネントをレンダリングする前に、ユーザーを認証する必要があります。SDK を初期化し、`TokenProvider` を設定したら、設定画面に [`AuthenticatorSettingsComponent`](/docs/ja-jp/get-started/universal-components/android/components/auth-methods-management) を追加します。これにより、ユーザーは MFA、パスキー、リカバリーコードをセルフサービスで包括的に管理できるようになります。
</Check>

<div id="learn-more">
  ## 詳細
</div>

<CardGroup cols={2}>
  <Card title="認証方法の管理" icon="shield" href="/docs/ja-jp/get-started/universal-components/android/components/auth-methods-management">
    `AuthenticatorSettingsComponent` のリファレンス、サポート対象の認証要素、Compose NavHost との連携について確認します。
  </Card>

  <Card title="スタイルとテーマをカスタマイズ" icon="palette" href="/docs/ja-jp/get-started/universal-components/android/android-theming">
    Auth0 のデザイントークンシステムを使用して、色、タイポグラフィ、間隔、角丸、サイズのトークンを上書きします。
  </Card>
</CardGroup>
