更新时间:2024-04-18 GMT+08:00
分享

密码认证

  • Java
    import com.huawei.ges.graph.v1.GESGraphClient;
    import com.huaweicloud.sdk.iam.v3.region.IamRegion;
    import com.huawei.ges.graph.v1.auth.password.GesGraphPasswordCredentials;
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.http.HttpConfig;
      //此处需要输出您的用户名,账号,项目ID
    String userName = "{userName}";
    String domainName = "{domainName}";
    String projectId = "{projectId}";
    // 认证用的密码硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
    // 本示例以密码保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_PWD。
    String password = System.getenv("HUAWEICLOUD_SDK_PWD");
    List<String> endpoints = IamRegion.valueOf(regionCode).getEndpoints();
    ICredential auth = new GesGraphPasswordCredentials(userName, domainName, password, projectId, iamEndPoints);
    HttpConfig httpConfig = new HttpConfig().withIgnoreSSLVerification(true);
    GESGraphClient client = GESGraphClient.newBuilder().withCredential(auth).withEndpoints(Arrays.asList(graphEndpoint)).withHttpConfig(httpConfig).build();
  • Python
    from gesgraphsdk.v1 import GESGraphClient
    from gesgraphsdk.v1.token_credentials import GesGraphTokenCredentials
    from huaweicloudsdkcore.http.http_config import HttpConfig
    
    graph_endpoint = "{graphEndpoint}"
    project_id = "{projectId}"
    iam_endpoint = "{imaEndpoint}"
    domain_name = "{domainName}"
    user_name = "{userName}"
    // 认证用的密码硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
    // 本示例以密码保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_PWD。
    String password = os.getenv("HUAWEICLOUD_SDK_PWD");
    auth = GesGraphTokenCredentials(iam_endpoint=iam_endpoint, domain_name=domain_name, user_name=user_name, password=password, project_id=project_id)
    http_conf = HttpConfig(ignore_ssl_verification=True, timeout=600)
    client = GESGraphClient.new_builder().with_credentials(auth).with_endpoint(graph_endpoint).with_http_config(http_conf).build()

相关文档