PHP:Auth0-PHPでManagement APIを使用する
Auth0 PHP SDKにはAuth0\SDK\API\Management
クラスが用意されています。このクラスには、Management APIにアクセスし、Auth0テナントで操作を実行するのに使用できるメソッドが格納されています。このインターフェイスを使用すると、以下のことが簡単にできます。
ユーザーの検索と作成
アプリケーションの作成と更新
ログエントリーの取得
ルールを管理する
このほかにも多数のことができます。詳細については、「APIリファレンス」を参照してください。認証
Management APIを使用するには、以下のいずれかの方法で認証を行う必要があります。
一時的にアクセスするかテストを実施する場合には、APIトークンを手動で生成し、
.env
ファイルに保存します。拡張アクセスでは、アクセスが必要な場合にはクライアント資格情報付与を作成して実行する必要があります。このプロセスの詳細については、Authentication APIページをご覧ください。
いずれの方式でも、生成されたトークンにはアプリで実行する操作に必要なスコープが含まれている必要があります。アクセスしようとする特定のエンドポイントに必要なスコープについては、APIドキュメントを参照してください。
必要なスコープを付与するには、以下のようにします。
[API] >[Auth0 Management API]> [Machine to Machine Applications(マシンツーマシンアプリケーション)]タブの順に移動します。
アプリケーションを見つけて認可します。
矢印をクリックして列を拡張し、必要なスコープを選択します。
上記のいずれかの方法を認証し、そのトークンを使って操作を実行できるようになります。
// 👆 We're continuing from the "getting started" guide linked in "Prerequisites" above. Append this to the index.php file you created there.
if (isset($env['AUTH0_MANAGEMENT_API_TOKEN'])) {
$auth0->configuration()->setManagementToken($env['AUTH0_MANAGEMENT_API_TOKEN']);
}
// Create a configured instance of the `Auth0\SDK\API\Management` class, based on the configuration we setup the SDK ($auth0) using.
// If no AUTH0_MANAGEMENT_API_TOKEN is configured, this will automatically perform a client credentials exchange to generate one for you, so long as a client secret is configured.
$management = $auth0->management();
Was this helpful?
Management
クラスはエンドポイントへのアクセスをインスタンスのファクトリメソッドとして保存します。たとえば、$management->users()
は/users Management APIエンドポイントの操作に使用できるAuth0\SDK\API\Management\Users
のインスタンスを返します。
例 - Search Users by Email(メールでユーザーを検索する)
このエンドポイントはこちらに記載されています。
// 👆 We're continuing from the code above. Append this to your source code file.
$response = $management->users()->getAll(['q' => 'josh']);
// Does the status code of the response indicate failure?
if ($response->getStatusCode() !== 200) {
die("API request failed.");
}
// Decode the JSON response into a PHP array:
$response = json_decode(response->getBody()->__toString(), true, 512, JSON_THROW_ON_ERROR);
if (! empty($response)) {
echo '<h2>User Results</h2>';
foreach ($response as $result) {
printf(
'<p><strong>%s</strong> <%s> - %s</p>',
!empty($result['nickname']) ? $result['nickname'] : 'No nickname',
!empty($result['email']) ? $result['email'] : 'No email',
$result['user_id']
);
}
}
Was this helpful?
例 - Get All Clients(すべてのクライアントを取得する)
このエンドポイントはこちらに記載されています。
// 👆 We're continuing from the code above. Append this to your source code file.
$response = $management->clients()->getAll(['q' => 'josh']);
// Does the status code of the response indicate failure?
if ($response->getStatusCode() !== 200) {
die("API request failed.");
}
// Decode the JSON response into a PHP array:
$response = json_decode(response->getBody()->__toString(), true, 512, JSON_THROW_ON_ERROR);
if (! empty($response)) {
echo '<h2>Get All Clients</h2>';
foreach ($response as $result) {
printf(
'<p><strong>%s</strong> - %s</p>',
$result['name'],
$result['client_id']
);
}
}
Was this helpful?