# huaweicloud-organizations-trusted-service-java **Repository Path**: HuaweiCloudDeveloper/huaweicloud-organizations-trusted-service-java ## Basic Information - **Project Name**: huaweicloud-organizations-trusted-service-java - **Description**: Organizations服务典型场景示例库。提供列出、禁用、使用可信服务 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master-dev - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-04-10 - **Last Updated**: 2025-06-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 版本说明 本示例配套的SDK版本为:3.1.35及以上版本 ### 示例简介 本示例展示如何使用组织(Organizations)相关SDK ### 功能介绍 组织服务可信服务管理相关操作 ### 前置条件 1.已 [注册](https://id1.cloud.huawei.com/UnifiedIDMPortal/portal/userRegister/regbyphone.html?themeName=red&access_type=offline&clientID=103493351&loginChannel=88000000&loginUrl=https%3A%2F%2Fauth.huaweicloud.com%2Fauthui%2Flogin.html%23&service=https%3A%2F%2Fauth.huaweicloud.com%2Fauthui%2FcasLogin&countryCode=cn&scope=https%3A%2F%2Fwww.huawei.com%2Fauth%2Faccount%2Funified.profile+https%3A%2F%2Fwww.huawei.com%2Fauth%2Faccount%2Frisk.idstate&reqClientType=88&state=cf496c0cb6c7414894276bebbfe1068c&lang=zh-cn) 华为云,并完成 [实名认证](https://account.huaweicloud.com/usercenter/?region=cn-north-4#/accountindex/realNameAuth) 2.获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。 3.已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 [访问密钥](https://support.huaweicloud.com/usermanual-ca/zh-cn_topic_0046606340.html) 。 4.已具备开发环境 ,支持Java JDK 1.8及其以上版本。 ### SDK获取和安装 您可以通过Maven配置所依赖的组织服务SDK 具体的SDK版本号请参见 [SDK开发中心](https://sdkcenter.developer.huaweicloud.com?language=java) (产品类别:组织) ### 代码示例 以下代码展示如何使用组织(Organizations)相关SDK ``` java // 用户身份认证 import com.huaweicloud.sdk.core.auth.GlobalCredentials; import com.huaweicloud.sdk.core.auth.ICredential; // 请求异常类 import com.huaweicloud.sdk.core.exception.ClientRequestException; // 导入待请求接口的 request 和 response 类 import com.huaweicloud.sdk.organizations.v1.OrganizationsClient; import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationRequest; import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationResponse; import com.huaweicloud.sdk.organizations.v1.model.DeleteOrganizationRequest; import com.huaweicloud.sdk.organizations.v1.model.DeleteOrganizationResponse; import com.huaweicloud.sdk.organizations.v1.model.DisableTrustedServiceRequest; import com.huaweicloud.sdk.organizations.v1.model.DisableTrustedServiceResponse; import com.huaweicloud.sdk.organizations.v1.model.EnableTrustedServiceRequest; import com.huaweicloud.sdk.organizations.v1.model.EnableTrustedServiceResponse; import com.huaweicloud.sdk.organizations.v1.model.ListTrustedServicesRequest; import com.huaweicloud.sdk.organizations.v1.model.ListTrustedServicesResponse; import com.huaweicloud.sdk.organizations.v1.model.ShowOrganizationRequest; import com.huaweicloud.sdk.organizations.v1.model.ShowOrganizationResponse; import com.huaweicloud.sdk.organizations.v1.model.TrustedServiceReqBody; // 日志打印 import com.huaweicloud.sdk.organizations.v1.region.OrganizationsRegion; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class OrganizationsTrustedServiceManagementDemo { private static final Logger logger = LoggerFactory.getLogger(OrganizationsTrustedServiceManagementDemo.class.getName()); public static void main(String[] args) { // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 String ak = System.getenv("HUAWEICLOUD_SDK_AK"); String sk = System.getenv("HUAWEICLOUD_SDK_SK"); ICredential auth = new GlobalCredentials().withAk(ak).withSk(sk); OrganizationsClient organizationsClient = OrganizationsClient.newBuilder() .withCredential(auth) .withRegion(OrganizationsRegion.valueOf("cn-north-4")) .build(); OrganizationsTrustedServiceManagementDemo demo = new OrganizationsTrustedServiceManagementDemo(); // 1、创建组织 demo.createOrganization(organizationsClient); // 2、查询所属组织信息 demo.showOrganization(organizationsClient); // 3、启用可信服务 demo.enableTrustedService(organizationsClient); // 4、列出组织的可信服务列表 demo.listTrustedServices(organizationsClient); // 5、禁用受信任服务 demo.disableTrustedService(organizationsClient); // 6、删除组织 demo.deleteOrganization(organizationsClient); } public void createOrganization(OrganizationsClient organizationsClient) { CreateOrganizationRequest createOrganizationRequest = new CreateOrganizationRequest(); try { CreateOrganizationResponse createOrganizationResponse = organizationsClient.createOrganization(createOrganizationRequest); logger.info(createOrganizationResponse.toString()); } catch (ClientRequestException e) { LogError(e); } } public void showOrganization(OrganizationsClient organizationsClient) { ShowOrganizationRequest showOrganizationRequest = new ShowOrganizationRequest(); try { ShowOrganizationResponse showOrganizationResponse = organizationsClient.showOrganization(showOrganizationRequest); logger.info(showOrganizationResponse.toString()); } catch (ClientRequestException e) { LogError(e); } } public void deleteOrganization(OrganizationsClient organizationsClient) { DeleteOrganizationRequest deleteOrganizationRequest = new DeleteOrganizationRequest(); try { DeleteOrganizationResponse deleteOrganizationResponse = organizationsClient.deleteOrganization(deleteOrganizationRequest); logger.info(deleteOrganizationResponse.toString()); } catch (ClientRequestException e) { LogError(e); } } public void enableTrustedService(OrganizationsClient organizationsClient) { EnableTrustedServiceRequest enableTrustedServiceRequest = new EnableTrustedServiceRequest() .withBody(new TrustedServiceReqBody().withServicePrincipal("service_principal")); //service_principal服务主体名称 try { EnableTrustedServiceResponse enableTrustedServiceResponse = organizationsClient.enableTrustedService(enableTrustedServiceRequest); logger.info(enableTrustedServiceResponse.toString()); } catch (ClientRequestException e) { LogError(e); } } public void listTrustedServices(OrganizationsClient organizationsClient) { ListTrustedServicesRequest listTrustedServicesRequest = new ListTrustedServicesRequest(); try { ListTrustedServicesResponse listTrustedServicesResponse = organizationsClient.listTrustedServices(listTrustedServicesRequest); logger.info(listTrustedServicesResponse.toString()); } catch (ClientRequestException e) { LogError(e); } } public void disableTrustedService(OrganizationsClient organizationsClient) { DisableTrustedServiceRequest disableTrustedServiceRequest = new DisableTrustedServiceRequest() .withBody(new TrustedServiceReqBody().withServicePrincipal("service_principal")); //service_principal服务主体名称 try { DisableTrustedServiceResponse disableTrustedServiceResponse = organizationsClient.disableTrustedService(disableTrustedServiceRequest); logger.info(disableTrustedServiceResponse.toString()); } catch (ClientRequestException e) { LogError(e); } } private static void LogError(ClientRequestException e) { logger.error("HttpStatusCode: " + e.getHttpStatusCode()); logger.error("RequestId: " + e.getRequestId()); logger.error("ErrorCode: " + e.getErrorCode()); logger.error("ErrorMsg: " + e.getErrorMsg()); } } ``` 您可以在 [组织Organizations服务文档](https://support.huaweicloud.com/productdesc-organizations/org_01_0011.html) 和[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/Organizations/doc?api=EnableTrustedService) 查看具体信息。 ### 修订记录 发布日期 | 文档版本 | 修订说明 :----: | :-----: | :------: 2023/4/10 |1.0 | 文档首次发布