以无影云电脑Java SDK查询便捷办公网络区的方法DescribeOfficeSites为例,介绍编写无影云电脑Java SDK的通用流程。
前提条件
您已经创建了AccessKey。具体操作,请参见创建AccessKey。
说明
为避免主账号泄露AccessKey带来的安全风险,建议您创建RAM用户,授予RAM用户无影云电脑相关的访问权限,再使用RAM用户的AccessKey调用SDK。具体操作,请参见通过RAM用户控制资源访问。
操作步骤
初始化配置对象com.aliyun.teaopenapi.models.Config。
Config对象存放AccessKeyId、AccessKeySecret和endpoint等配置,如示例中的endpoint为ecd.cn-hangzhou.aliyuncs.com。更多关于地域的信息,请参见地域和可用区。
Config config = new Config() // 您的AccessKey ID .setAccessKeyId(accessKeyId) // 您的AccessKey Secret .setAccessKeySecret(accessKeySecret); // 访问的域名 config.endpoint = "ecd.cn-hangzhou.aliyuncs.com";
从com.aliyun.ecd20200930.Client类中生成对象client。
后续从com.aliyun.ecd20200930.models中获得request和response。
Client client = new Client(config);
创建对应API的Request,类的命名规则为API方法名加上Request。
示例如下:
CreateADConnectorDirectoryRequest request = new CreateADConnectorDirectoryRequest();
设置请求类request的参数。
通过request类的属性设置必要的信息,即API中必须要提供的信息。
request.setRegionId("your_value")
通过client对象获得对应request响应response。
DescribeOfficeSitesResponse describeOfficeSitesResponse = client.describeOfficeSites(describeOfficeSitesRequest); System.out.println(new Gson().toJson(describeOfficeSitesResponse.getBody()));
调用response中对应的属性获得返回的参数值。
假设您需要获取requestId:
String requestId = describeOfficeSitesResponse.getBody().getRequestId();
使用catch()处理报错。
catch (Exception e) { e.printStackTrace(); }
执行结果
全部返回信息如下所示:
{
"headers":{
"access-control-allow-origin":"*",
"date":"Wed, 10 Aug 2022 08:55:44 GMT",
"content-length":"788",
"access-control-max-age":"172800",
"x-acs-request-id":"01992A5F-4066-56CF-A834-FF510E17F822","access-control-allow-headers":"X-Requested-With, X-Sequence, _aop_secret, _aop_signature, x-acs-action, x-acs-version, x-acs-date, Content-Type",
"connection":"keep-alive",
"content-type":"application/json;charset\u003dutf-8",
"access-control-allow-methods":"POST, GET, OPTIONS, PUT, DELETE","x-acs-trace-id":"eb14d97daef3840d6b69bbe42eb32aa4"},
"body":{
"nextToken":"AAAAAV3MpHK1AP0pfERHZN5pu6nlcoiW7skgMsXzqaU1HSXW",
"officeSites":[
{"ADConnectors":[],
"bandwidth":23,
"cidrBlock":"192.168.2.0/24",
"cloudBoxOfficeSite":false,
"creationTime":"2022-07-11T06:10Z",
"desktopAccessType":"INTERNET",
"desktopCount":2,
"dnsAddress":[],
"enableAdminAccess":true,
"enableCrossDesktopAccess":false,
"enableInternetAccess":true,
"fileSystemIds":[],
"mfaEnabled":true,
"name":"simple-开通互联网",
"needVerifyLoginRisk":true,
"needVerifyZeroDevice":true,
"networkPackageId":"np-bruxzr9vzlod9pu42",
"officeSiteId":"cn-hangzhou+dir-892021****",
"officeSiteType":"SIMPLE",
"ouName":"",
"ssoEnabled":false,
"status":"REGISTERED",
"subDnsAddress":[],
"vSwitchIds":[],
"vpcId":"vpc-bp1j8nw5ln4oxve4t****",
"vpcType":"standard"}],
"requestId":"01992A5F-4066-56CF-A834-FF510E17F822"}
}
完整代码示例
以下为本文示例的完整Java SDK代码。
import com.aliyun.ecd20200930.models.DescribeOfficeSitesRequest;
import com.aliyun.ecd20200930.models.DescribeOfficeSitesResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.google.gson.Gson;
import java.util.ArrayList;
public class Sample {
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.ecd20200930.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的 AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的 AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "ecd.cn-hangzhou.aliyuncs.com";
return new com.aliyun.ecd20200930.Client(config);
}
public static void main(String[] args) throws Exception {
com.aliyun.ecd20200930.Client client = Sample.createClient("your_ak", "your_aks");
DescribeOfficeSitesRequest describeOfficeSitesRequest = new DescribeOfficeSitesRequest()
.setRegionId("your_value");
try {
DescribeOfficeSitesResponse describeOfficeSitesResponse = client.describeOfficeSites(describeOfficeSitesRequest);
System.out.println(new Gson().toJson(describeOfficeSitesResponse));
String requestId = describeOfficeSitesResponse.getBody().getRequestId();
System.out.println(requestId);
} catch (TeaException error) {
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
文档内容是否对您有帮助?