目前提供 Golang、Java两种语言版本的SDK。
在使用 SDK 调用 OpenAPI 过程中不需要传入 ApiAction 和 ApiVersion,只需要在构造 Client 时指定 basePath,AK 和 SK,或者传入 《权限相关接口》1.获取用户临时Token接口获得的临时 AK SK 和 token 构造; 详细见 SDK 说明和示例:
注意
使用的 SDK 的大版本须与CDP的大版本保持一致。
环境要求:
Go 环境版本必须不低于 1.15
安装cdp SDK for Go:
go get -u github.com/volcengine/cdp-openapi-sdk-go@v1.19.0
设置安全凭证和连接超时等配置
var basePath = "https://XXX/open_platform/openapi" var accessKeyId = "ak" var accessKeySecret = "sk" httpCLient := http.Client{Timeout: 1 * time.Second} //初始化配置对象 Config := Configuration{AccessKeyId: accessKeyId, AccessKeySecret: accessKeySecret, BasePath: basePath, HTTPClient: &httpCLient} // 使用 6.1 接口获得的临时 ak sk 和 token 构造连接配置 Config := Configuration{AccessKeyId: "ak", AccessKeySecret: "sk", SessionToken: "token", BasePath: "basePath", HTTPClient: &httpCLient} //实例化客户端 client, err := NewAPIClient(&Config)
文档中示例代码仅供参考
分群相关API
1. 分群列表
opts := SegmentationApiLegacyGetSegmentListOpts{Current: optional.NewInt32(1), PageSize: optional.NewInt32(20)} responseBody, httpRespose, err := client.SegmentationApi.LegacyGetSegmentList(context.Background(), 1, &opts)
2. 分群详细信息
tenantId := 1 segId : =123 responseBody, httpRespose, err := client.SegmentationApi.LegacyGetSegment(context.Background(), tenantId, segId)
代码仓库:
开源Github: https://github.com/volcengine/cdp-openapi-sdk-java
Maven: https://search.maven.org/search?q=a:cdp-openapisdk-java
添加依赖
<dependency> <groupId>com.volcengine</groupId> <artifactId>cdp-openapisdk-java</artifactId> <version>1.19.0</version> </dependency> Version同CDP版本
使用示例
// 通过渠道账号获取的 AK SK 来访问(不推荐) private final ApiClient client = new ApiClient( "ak", "sk", "https://xxxx/open_platform/openapi" ); // 使用基于 STS 的方式来访问(推荐)见 1.2.2.2 步骤中获取 App 秘钥(ak,sk),另外需要传入绑定账号名 private final ApiClient client = new ApiClient( "ak", "sk", "account" "https://xxxx/open_platform/openapi" ); private final SegmentationApi segClient = new SegmentationApi(client); @Test public void sdkTest() throws Exception { try { File file = segClient.downloadSegFile(1, 1000008, "txt", false); System.out.println(file.getName()); ByteDanceResponseSegmentationUploadResp res = segClient.uploadSegFile(file, 1); System.out.println(res); } catch (ApiException e) { throw e; } } @Test public void sdkAsyncTest() throws Exception { try { segClient.legacyGetSegmentListAsync( 1, 1, 10, null, null, null, null, null, null, null, null, new ApiCallback<ByteDanceResponseSegmentationListResp>() { public void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {} public void onSuccess(ByteDanceResponseSegmentationListResp result, int statusCode, Map<String, List<String>> responseHeaders) { System.out.println(result); } public void onUploadProgress(long bytesWritten, long contentLength, boolean done) {} public void onDownloadProgress(long bytesRead, long contentLength, boolean done) {} }); Thread.sleep(5000); } catch (ApiException e) { throw e; } }