oo

アカウントAPIの基本

Liferay DXP/Portal 7.4以降

アプリケーションメニューから アカウント を管理することができますが、LiferayのREST APIを利用することもできます。 これらのサービスを呼び出して、アカウントの作成や管理を行います。

アカウントの追加

新しいLiferay DXPインスタンスを起動し、以下を実行します。

docker run -it -m 8g -p 8080:8080 liferay/dxp:2024.q1.1。

メールアドレス test@liferay.com とパスワード test を使用して、http://localhost:8080でLiferayにサインインしてください。 プロンプトが表示されたら、パスワードを learn に変更します。

次に、以下の手順に従います。

  1. Accounts API Basics をダウンロードして解凍する。

    curl https://resources.learn.liferay.com/dxp/latest/en/users-and-permissions/developer-guide/liferay-t5p9.zip -O
    
    unzip liferay-t5p9.zip
    
  2. cURLスクリプトを使用して、インスタンスに新しいAccountを追加します。 コマンドラインで curl フォルダに移動します。 Account_POST_ToInstance.shスクリプトを実行する。

    ./Account_POST_ToInstance.sh
    

    JSONレスポンスでは、新しいAccountが追加されたことを示しています。

    {
      "description" : "Foo",
      "domains" : [ ],
      "externalReferenceCode" : "",
      "id" : 39302,
      "name" : "Able",
      "numberOfUsers" : 0,
      "organizationIds" : [ ],
      "parentAccountId" : 0,
      "status" : 0,
      "type" : "business"
    }
    
  3. Global MenuApplicationsAccounts に移動します。 新しいアカウントが追加されたことを確認してください。

    See that a new account has been added.

  4. RESTサービスは、Javaクライアントを使って呼び出すこともできます。 curlフォルダからjava フォルダに移動します。 以下のコマンドでソースファイルをコンパイルします。

    javac -classpath .:* *.java
    
  5. 以下のコマンドでAccount_POST_ToInstance.javaクラスを実行する。

    java -classpath .:* Account_POST_ToInstance
    

cURLコマンドの検証

Account_POST_ToInstance.shスクリプトはcURLコマンドでRESTサービスを呼び出す。

curl \
	"http://localhost:8080/o/headless-admin--userser/v1.0/accounts" \
	--data-raw '
		{
			"description": "Foo",
			"name": "Able"
		}' \
	--header "Content-Type: application/json" \
	--request "POST" \
	--user "test@liferay.com:learn"

ここでは、コマンドの引数を紹介します。

引数 説明
-H "Content-Type: application/json" リクエストボディのフォーマットがJSONであることを示します。
-X POST 指定されたエンドポイントで起動するHTTPメソッド
"http://localhost:8080/o/headless-admin-user/v1.0/accounts" RESTサービスのエンドポイント
-d "{"description": "Foo", "name": "Able"}" お客様が掲載を希望するデータ
-u "test@liferay.com:learn" 基本的な認証情報
Note

ここでは、デモのために基本的な認証を使用しています。 本番環境では、 OAuth2 を介してユーザーを認証する必要があります。 OAuth2 を使用する React アプリケーションのサンプルについては、 OAuth2によるユーザーの認証 を参照してください。

他のcURLコマンドも同様のJSON引数を使用しています。

Javaクラスを調べる

Account_POST_ToInstance.javaクラスは、Account関連サービスを呼び出してアカウントを追加する。

public static void main(String[] args) throws Exception {
	AccountResource.Builder builder = AccountResource.builder();

	AccountResource accountResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Account account = accountResource.postAccount(
		new Account() {
			{
				description = "Foo";
				name = "Fox";
			}
		});

	System.out.println(account);
}

このクラスは、わずか3行のコードでRESTサービスを呼び出します。

行(省略形) 説明
AccountResource.Builder builder = ... AccountResourceサービスインスタンスを生成するためのBuilder を取得する。
AccountResource accountResource = builder.authentication(...).build(); 基本認証を指定し、AccountResource サービスインスタンスを生成する。
Account account = accountResource.postAccount(...); accountResource.postAccountメソッドを呼び出し、データをpostに渡す。

このプロジェクトには依存関係として com.liferay.headless.admin.user.client.jar ファイルが含まれていることに注意してください。 すべての REST アプリケーションのクライアント JAR 依存情報は、インストー ルの API エクスプローラーの /o/api にある。

Note

mainメソッドのコメントは、クラスの実行を示している。

他のJavaクラスの例もこれと似ているが、異なるAccountResourceメソッドを呼び出している。

Important

サービスの詳細は AccountResource を参照。

以下は、cURLとJavaを使って他のAccount RESTサービスを呼び出す例である。

インスタンスからアカウントを取得

以下のcURLまたはJavaコマンドを実行することで、アカウントを一覧表示することができます。

Accounts_GET_FromInstance.sh

コマンド:

./Accounts_GET_FromInstance.sh

コード:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/accounts" \
	--user "test@liferay.com:learn"

Accounts_GET_FromInstance.java

コマンド:

java -classpath .:* Accounts_GET_FromInstance

コード:

public static void main(String[] args) throws Exception {
	AccountResource.Builder builder = AccountResource.builder();

	AccountResource accountResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Page<Account> page = accountResource.getAccountsPage(
		null, null, Pagination.of(1, 2), null);

	System.out.println(page);
}

インスタンスの Account オブジェクトは JSON で表示される。

アカウントの取得

以下のcURLまたはJavaコマンドで特定のアカウントを取得します。

Tip

インスタンスの アカウントID を取得するには Accounts_GET_FromInstance.[java|sh] を使用してください。

Account_GET_ById.sh

コマンド:

./Account_GET_ById.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-admin--userser/v1.0/accounts/${1}" \
	--user "test@liferay.com:learn"

Account_GET_ById.java

コマンド:

java -classpath .:* -DaccountId=1234 Account_GET_ById

コード:

public static void main(String[] args) throws Exception {
	AccountResource.Builder builder = AccountResource.builder();

	AccountResource accountResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	System.out.println(
		accountResource.getAccount(
			Long.valueOf(System.getProperty("accountId"))));
}

AccountフィールドはJSONで表示される。

アカウントへのパッチ適用

以下のcURLおよびJavaコマンドで、既存のAccountの部分編集を行います。 1234`をあなたのアカウントIDに置き換えてください。

Account_PATCH_ById.sh

コマンド:

./Account_PATCH_ById.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-admin--userser/v1.0/accounts/${1}" \
	--data-raw '
		{
			"description": "Bar"
		}' \
	--header "Content-Type: application/json" \
	--request "PATCH"	 \
	--user "test@liferay.com:learn"

Account_PATCH_ById.java

コマンド:

java -classpath .:* -DaccountId=1234 Account_PATCH_ById

コード:

public static void main(String[] args) throws Exception {
	AccountResource.Builder builder = AccountResource.builder();

	AccountResource accountResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Account account = accountResource.patchAccount(
		Long.valueOf(System.getProperty("accountId")),
		new Account() {
			{
				description = "Bar";
			}
		});

	System.out.println(account);
}

アカウントの作成

以下のcURLとJavaコマンドで、既存のAccountを完全に上書きします。 1234をあなたのアカウントIDに置き換えてください。

Account_PUT_ById.sh

コマンド:

./Account_PUT_ById.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-admin--userser/v1.0/accounts/${1}" \
	--data-raw '
		{
			"description": "Goo",
			"name": "Baker"
		}' \
	--header "Content-Type: application/json" \
	--request "PUT" \
	--user "test@liferay.com:learn"

Account_PUT_ById.java

コマンド:

java -classpath .:* -DaccountId=1234 Account_PUT_ById

コード:

public static void main(String[] args) throws Exception {
	AccountResource.Builder builder = AccountResource.builder();

	AccountResource accountResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Account account = accountResource.putAccount(
		Long.valueOf(System.getProperty("accountId")),
		new Account() {
			{
				description = "Goo";
				name = "George";
			}
		});

	System.out.println(account);
}

アカウントの削除

以下のcURLおよびJavaコマンドで既存のAccountを削除します。 1234をあなたのアカウントIDに置き換えてください。

Account_DELETE_ById.sh

コマンド:

./Account_DELETE_ById.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-admin--userser/v1.0/accounts/${1}" \
	--request "DELETE" \
	--user "test@liferay.com:learn"

Account_DELETE_ById.java

コマンド

java -classpath .:* -DaccountId=1234 Account_DELETE_ById

コード:

public static void main(String[] args) throws Exception {
	AccountResource.Builder builder = AccountResource.builder();

	AccountResource accountResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	accountResource.deleteAccount(
		Long.valueOf(System.getProperty("accountId")));
}

API Explorer はすべての Account サービスとスキーマを表示し、各サービスを試すためのインターフェイスを備えている。

Note

PostalAddress_POST_ToAccount および PostalAddresses_GET_FromAccount を使って、アカウントの郵便住所を作成および取得します。