You need to enable JavaScript to run this app.
导航
SDK使用
最近更新时间:2024.09.09 14:36:48首次发布时间:2023.10.08 10:43:29

1. 概述

目前提供 Golang、Java两种语言版本的SDK。
在使用 SDK 调用 OpenAPI 过程中不需要传入 ApiAction 和 ApiVersion,只需要在构造 Client 时指定 basePath,AK 和 SK,或者传入 《权限相关接口》1.获取用户临时Token接口获得的临时 AK SK 和 token 构造; 详细见 SDK 说明和示例:

注意

使用的 SDK 的大版本须与CDP的大版本保持一致。

2. SDK使用

2.1 Golang SDK

  1. 获取与安装

环境要求:

  1. 相关配置并初始化客户端

设置安全凭证和连接超时等配置

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)
  1. API调用示例

文档中示例代码仅供参考
分群相关API

  1. 分群列表
opts := SegmentationApiLegacyGetSegmentListOpts{Current: optional.NewInt32(1), PageSize: optional.NewInt32(20)}
responseBody, httpRespose, err := client.SegmentationApi.LegacyGetSegmentList(context.Background(), 1, &opts)
  1. 分群详细信息
tenantId := 1
segId : =123
responseBody, httpRespose, err := client.SegmentationApi.LegacyGetSegment(context.Background(), tenantId, segId)

2.2 Java SDK

代码仓库:
开源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;
    }
}