Updated on 2024-11-25 GMT+08:00

Authentication Mode

There are three ways to initialize the client based on different authentication methods. You can choose one according to your needs.

AK/SK-based Authentication

For how to set ak, sk, regionCode, and graphEndpoint, see Obtaining Initialization Parameters.

import com.huawei.ges.graph.v1.GESGraphClient; // Memory edition client
import com.huawei.ges.graph.v1.persistence.GESGraphPersistenceClient; // Database edition client
import com.huawei.ges.graph.v1.auth.aksk.GesGraphAkSkCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.http.HttpConfig;
import java.util.Arrays;


// Hard-coded or plaintext AK and SK are insecure. So, encrypt your AK and SK and store them in the configuration file or environment variables.
// In this example, the AK and SK are stored in environment variables. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
String ak = System.getenv("HUAWEICLOUD_SDK_AK");
String sk = System.getenv("HUAWEICLOUD_SDK_SK");
String regionCode = "";
String graphEndpoint = "";
ICredential auth = new GesGraphAkSkCredentials().withAk(ak).withSk(sk).withRegionCode(regionCode);
HttpConfig httpConfig = HttpConfig.getDefaultHttpConfig();

// Memory edition client
GESGraphClient gesGraphClient = GESGraphClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build();
// Database edition client
GESGraphPersistenceClient gesGraphPersistenceClient = GESGraphPersistenceClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build();

Password-based Authentication

For how to set domainName, userName, password, projectId, iamEndPoint, and graphEndpoint, see Obtaining Initialization Parameters.

import com.huawei.ges.graph.v1.GESGraphClient; // Memory edition client
import com.huawei.ges.graph.v1.persistence.GESGraphPersistenceClient; // Database edition client
import com.huawei.ges.graph.v1.auth.password.GesGraphPasswordCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.http.HttpConfig;
import java.util.Arrays;


// Hard-coded or plaintext password is insecure. So, encrypt your password and store it in the configuration file or environment variables.
// In this example, the password is stored in environment variables. Before running this example, set the environment variable HUAWEICLOUD_SDK_PWD.
String password = System.getenv("HUAWEICLOUD_SDK_PWD");
String domainName = "";
String userName = "";
String projectId = "";
String iamEndPoint = "";
String graphEndpoint = "";
ICredential auth = new GesGraphPasswordCredentials(userName, domainName, password, projectId, Arrays.asList(iamEndPoint));
HttpConfig httpConfig = HttpConfig.getDefaultHttpConfig();

// Memory edition client
GESGraphClient gesGraphClient = GESGraphClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build();
// Database edition client
GESGraphPersistenceClient gesGraphPersistenceClient = GESGraphPersistenceClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build();

Token-based Authentication

For how to set authToken and graphEndpoint, see Obtaining Initialization Parameters.

import com.huawei.ges.graph.v1.GESGraphClient; // Memory edition client
import com.huawei.ges.graph.v1.persistence.GESGraphPersistenceClient; // Database edition client
import com.huawei.ges.graph.v1.auth.token.GesGraphTokenCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.http.HttpConfig;
import java.util.Arrays;


String authToken = "";
String graphEndpoint = "";
ICredential auth = new GesGraphTokenCredentials().withXAuthToken(authToken);
HttpConfig httpConfig = HttpConfig.getDefaultHttpConfig();

// Memory edition client
GESGraphClient gesGraphClient = GESGraphClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build();
// Database edition client
GESGraphPersistenceClient gesGraphPersistenceClient = GESGraphPersistenceClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build();