更新时间:2024-11-25 GMT+08:00
认证方式
根据不同的认证方式,客户端初始化有三种方式,可根据需要选择其中一种。
AK/SK认证
参数ak、sk、regionCode和graphEndpoint如何填写见初始化参数获取。
import com.huawei.ges.graph.v1.GESGraphClient; // 内存版客户端 import com.huawei.ges.graph.v1.persistence.GESGraphPersistenceClient; // 持久化版客户端 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; // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全 // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和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(); // 内存版客户端 GESGraphClient gesGraphClient = GESGraphClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build(); // 持久化版客户端 GESGraphPersistenceClient gesGraphPersistenceClient = GESGraphPersistenceClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build();
密码认证
参数domainName、userName、password、projectId 、iamEndPoint和graphEndpoint如何填写见初始化参数获取。
import com.huawei.ges.graph.v1.GESGraphClient; // 内存版客户端 import com.huawei.ges.graph.v1.persistence.GESGraphPersistenceClient; // 持久化版客户端 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; // 认证用的密码硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全 // 本示例以密码保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量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(); // 内存版客户端 GESGraphClient gesGraphClient = GESGraphClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build(); // 持久化版客户端 GESGraphPersistenceClient gesGraphPersistenceClient = GESGraphPersistenceClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build();
Token认证
参数authToken和graphEndpoint如何填写见初始化参数获取。
import com.huawei.ges.graph.v1.GESGraphClient; // 内存版客户端 import com.huawei.ges.graph.v1.persistence.GESGraphPersistenceClient; // 持久化版客户端 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(); // 内存版客户端 GESGraphClient gesGraphClient = GESGraphClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build(); // 持久化版客户端 GESGraphPersistenceClient gesGraphPersistenceClient = GESGraphPersistenceClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build();
父主题: 客户端初始化