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

# Management API を使う

> SDK の Management クラスを使用して PHP で Auth0 の Management API にアクセスし、ユーザーの検索、アプリケーションの管理、テナント操作を実行します。

> [!WARNING]
>
> **Auth0 PHP SDK v9機能は現在、です。**

Auth0 PHP SDK には `Auth0\SDK\API\Management` クラスが用意されており、[Management API](https://auth0.com/docs/ja-jp/api/management/v2.md) にアクセスして Auth0 テナントに対する操作を実行するためのメソッドが含まれています。このインターフェースを使用すると、次のようなことを簡単に行えます。

* ユーザーの検索と作成
* アプリケーションの作成と更新
* ログエントリの取得
* ルールの管理

... など、さまざまな操作が可能です。利用可能な機能については、[API リファレンス](https://auth0.com/docs/ja-jp/api/management/v2.md) をご覧ください。

## 認証

Management API (Management API: お客様が管理タスクを実行できるようにするプロダクトです。) を使用するには、次の 2 つの方法のいずれかで認証する必要があります。

* 一時的なアクセスやテストには、[API トークンを手動で生成](https://auth0.com/docs/ja-jp/secure/tokens/access-tokens/management-api-access-tokens.md) し、`.env` ファイルに保存できます。
* 継続的にアクセスするには、必要なときにクライアントクレデンシャルグラントを作成して実行する必要があります。この手順については、[Authentication API ページ](https://auth0.com/docs/ja-jp/libraries/auth0-php/using-the-authentication-api-with-auth0-php.md) で詳しく説明しています。

どちらの方法でも、生成されるトークンには、アプリが実行する操作に必要なスコープが含まれている必要があります。アクセスしようとしている特定のエンドポイントに必要なスコープについては、[API ドキュメント](https://auth0.com/docs/ja-jp/api/management/v2.md) を参照してください。

必要なスコープを付与するには、次の手順に従います。

1. [APIs](https://manage.auth0.com/#/apis) > Auth0 Management API > **Machine to Machine Applications** タブに移動します。
2. 対象のアプリケーションを見つけて認可します。
3. 矢印をクリックして行を展開し、必要なスコープを選択します。

これで、上記 2 つの方法のいずれかで認証し、そのトークンを使って操作を実行できます。

```php lines theme={null}
$management = new \Auth0\SDK\API\Management\Wrapper\ManagementClient(
    new \Auth0\SDK\API\Management\Wrapper\ManagementClientOptions(
        domain: $_ENV['AUTH0_DOMAIN'],
        clientId: $_ENV['AUTH0_CLIENT_ID'],
        clientSecret: $_ENV['AUTH0_CLIENT_SECRET'],
    ),
);
```

`ManagementClient` はエンドポイントをプロパティとして公開します (たとえば、`$management->users` や `$management->clients`) 。結果は型付きオブジェクトとして返されます。生の配列を展開するのではなく、`getEmail()` や `getName()` などの名前付きメソッドを呼び出して値を取得します。

### 例 - メールアドレスでユーザーを検索

このエンドポイントのドキュメントは[こちら](https://auth0.com/docs/ja-jp/api/management/v2/users/get-users.md)をご覧ください。

```php lines theme={null}
$users = $management->users->list(
    new \Auth0\SDK\API\Management\Users\Requests\ListUsersRequestParameters([
        'q'             => 'email:j*',
        'perPage'       => 5,
        'includeTotals' => true,
    ]),
);

foreach ($users as $user) {
    printf("%s <%s> - %s\n", $user->getNickname() ?? 'No nickname', $user->getEmail() ?? 'No email', $user->getUserId());
}
```

### 例 - すべてのクライアントを取得

このエンドポイントのドキュメントは[こちら](https://auth0.com/docs/ja-jp/api/management/v2/clients/get-clients.md)をご覧ください。

```php lines theme={null}
$clients = $management->clients->list(
    new \Auth0\SDK\API\Management\Clients\Requests\ListClientsRequestParameters([
        'perPage'       => 5,
        'includeTotals' => true,
    ]),
);

foreach ($clients as $client) {
    printf("%s - %s\n", $client->getName(), $client->getClientId());
}
```

## 詳細情報

* [Auth0-PHP を使い始める](https://auth0.com/docs/ja-jp/libraries/auth0-php.md)
* [Login、Logout、ユーザープロファイルを管理する](https://auth0.com/docs/ja-jp/libraries/auth0-php/auth0-php-basic-use.md)
* [Authentication API を使用する](https://auth0.com/docs/ja-jp/libraries/auth0-php/using-the-authentication-api-with-auth0-php.md)
* [JWT (JSON Web Tokens) を検証する](https://auth0.com/docs/ja-jp/libraries/auth0-php/validating-jwts-with-auth0-php.md)
* [Auth0-PHP 連携のトラブルシューティング](https://auth0.com/docs/ja-jp/libraries/auth0-php/troubleshoot-auth0-php-library.md)
