更新时间:2024-11-25 GMT+08:00
分享

认证方式

AK/SK认证

参数ak、sk、regionCode和graphEndpoint如何填写见初始化参数获取

import os
from gesgraphsdk.v1.aksk_credentials import GesGraphAkSkCredentials
from gesgraphsdk.v1.gesgraph_client import GESGraphClient  # 内存版客户端
from gesgraphsdk.v1.persistence.gesgraphpersistence_client import GESGraphPersistenceClient  # 持久化版客户端
from huaweicloudsdkcore.http.http_config import HttpConfig


# 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全
# 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK
ak = os.getenv("HUAWEICLOUD_SDK_AK")
sk = os.getenv("HUAWEICLOUD_SDK_SK")
region_code = "{regionCode}" 
graph_endpoint = "{graphEndpoint}"
auth = GesGraphAkSkCredentials(ak=ak, sk=sk, region_code=region_code)
http_conf = HttpConfig.get_default_config()

# 内存版客户端
ges_graph_client = GESGraphClient.new_builder().with_credentials(auth).with_endpoint(graph_endpoint).with_http_config(http_conf).build();
# 持久化版客户端
ges_graph_persistence_client = GESGraphPersistenceClient.new_builder().with_credentials(auth).with_endpoint(graph_endpoint).with_http_config(http_conf).build();

密码认证

参数domainName、userName、password、projectId 、iamEndPoint和graphEndpoint如何填写见初始化参数获取

import os
from gesgraphsdk.v1.gesgraph_client import GESGraphClient  # 内存版客户端
from gesgraphsdk.v1.persistence.gesgraphpersistence_client import GESGraphPersistenceClient  # 持久化版客户端
from gesgraphsdk.v1.token_credentials import GesGraphTokenCredentials
from huaweicloudsdkcore.http.http_config import HttpConfig


// 认证用的密码硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全
// 本示例以密码保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_PWD
password = os.getenv("HUAWEICLOUD_SDK_PWD")
domain_name = "{domainName}"
user_name = "{userName}"
project_id = "{projectId}"
graph_endpoint = "{graphEndpoint}"
iam_endpoint = "{iamEndPoint}"
auth = GesGraphTokenCredentials(iam_endpoint=iam_endpoint, domain_name=domain_name, user_name=user_name, password=password, project_id=project_id)
http_conf = HttpConfig.get_default_config()

# 内存版客户端
ges_graph_client = GESGraphClient.new_builder().with_credentials(auth).with_endpoint(graph_endpoint).with_http_config(http_conf).build()
# 持久化版客户端
ges_graph_persistence_client = GESGraphPersistenceClient.new_builder().with_credentials(auth).with_endpoint(graph_endpoint).with_http_config(http_conf).build();

Token认证

参数authToken和graphEndpoint如何填写见初始化参数获取

from gesgraphsdk.v1.gesgraph_client import GESGraphClient  # 内存版客户端
from gesgraphsdk.v1.persistence.gesgraphpersistence_client import GESGraphPersistenceClient  # 持久化版客户端
from gesgraphsdk.v1.token_credentials import GesGraphTokenCredentials
from huaweicloudsdkcore.http.http_config import HttpConfig


graph_endpoint = "{graphEndpoint}"
token = "{authToken}"
auth = GesGraphTokenCredentials(token=token)
http_conf = HttpConfig.get_default_config()

# 内存版客户端
ges_graph_client = GESGraphClient.new_builder().with_credentials(auth).with_endpoint(graph_endpoint).with_http_config(http_conf).build()
# 持久化版客户端
ges_graph_persistence_client GESGraphPersistenceClient.new_builder().with_credentials(auth).with_endpoint(graph_endpoint).with_http_config(http_conf).build();

相关文档