C SDK 入门
本页内容
在自己的服务端使用 C SDK,完成源码下载、凭据配置、身份查询和业务应用读取。
运行环境与下载
准备 C11、CMake 3.20+,需原生依赖。下载C 源码包,用 SHA256SUMS 校验解压后的文件。需要固定接入版本时,保存所用源码包及其独立摘要。
当前 C 源码预览版本为 0.8.0-preview.1。还需要 libcurl 7.85+、OpenSSL 3+、cJSON 1.7.18+、PCRE2 10.42+ 和 pkg-config。文件传输助手的支持环境见下方专题;HTTP 客户端和辅助 CLI 的用途不同。
步骤一 准备示例项目
在自己的工作目录解压。保留包内目录结构,确认已包含首次运行的入口文件 example.c。
保留根目录 CMakeLists.txt 与 cloudsign-sdk/c/,按包内依赖说明构建。首次调用的 cloudsign_api_example 只查当前身份。
步骤二 配置网站地址与凭据
在运行示例的终端或服务进程中设置下列变量。查看 AccessKey 的获取与保存步骤。
| 变量 | 填写方式 |
|---|---|
CLOUDSIGN_BASE_URL | https://www.onesignios.com,完整 HTTPS 网站来源,不带 API 路径。 |
CLOUDSIGN_ACCESS_KEY_ID | 自己的 AccessKey ID。 |
CLOUDSIGN_ACCESS_KEY_SECRET | 对应 Secret,从服务端安全配置注入。 |
CLOUDSIGN_SECURITY_TOKEN | 临时凭据使用同一组令牌;长期凭据不填写。 |
如果已按快速入门配置了凭据,可在同一终端继续使用。SDK 的 CLOUDSIGN_BASE_URL 填完整 HTTPS 来源。从 PowerShell 或 Bash 配置方式见首次调用。
export CLOUDSIGN_BASE_URL="https://www.onesignios.com"Windows PowerShell
$env:CLOUDSIGN_BASE_URL = "https://www.onesignios.com"当前示例只查询身份。执行写入前,请核对具体接口的权限,按写入准备说明保存幂等键。
步骤三 运行只读示例
在解压根目录直接运行本节命令。下方展示包内入口文件的内容,供集成到自己的项目时参考。示例调用 查询当前身份与权限,用于核对这套凭据的所属账号和授权范围。
/* C11, trusted server. Save as example.c in the source ZIP root. */
#include <cloudsign/openapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int copy_env(char *out, size_t capacity, const char *name, int required) {
const char *value = getenv(name);
if (!value || !*value) return !required;
size_t length = strlen(value);
if (length >= capacity) return 0;
memcpy(out, value, length + 1);
return 1;
}
int main(void) {
cloudsign_credentials credentials = {0};
cloudsign_client *client = NULL;
cloudsign_result *result = NULL;
cloudsign_decoded_response *decoded = NULL;
cloudsign_built_request *built = NULL;
cloudsign_status status = CLOUDSIGN_INVALID_CONFIGURATION;
int initialized = 0, exit_code = 1;
const char *origin = getenv("CLOUDSIGN_BASE_URL");
if (!origin || !copy_env(credentials.access_key_id, sizeof(credentials.access_key_id), "CLOUDSIGN_ACCESS_KEY_ID", 1)
|| !copy_env(credentials.access_key_secret, sizeof(credentials.access_key_secret), "CLOUDSIGN_ACCESS_KEY_SECRET", 1)
|| !copy_env(credentials.security_token, sizeof(credentials.security_token), "CLOUDSIGN_SECURITY_TOKEN", 0)) goto done;
status = cloudsign_global_init();
if (status != CLOUDSIGN_OK) goto done;
initialized = 1;
cloudsign_options options = {.origin = origin, .credentials = &credentials, .timeout_ms = 15000};
status = cloudsign_client_create(&options, &client);
cloudsign_credentials_clear(&credentials);
if (status != CLOUDSIGN_OK) goto done;
const cloudsign_input_get_identity input = {0};
status = cloudsign_build_get_identity(&input, &built);
if (status != CLOUDSIGN_OK) goto done;
status = cloudsign_send_built_request(client, built, &result);
if (status == CLOUDSIGN_OK) {
status = cloudsign_decode_get_identity(result, &decoded);
if (status != CLOUDSIGN_OK) goto done;
const cloudsign_model_get_identity_data *data = cloudsign_response_get_identity(decoded);
if (!data) { status = CLOUDSIGN_INVALID_RESPONSE; goto done; }
/* Read typed fields here; NULL/absent is distinct from zero/false. */
(void)data;
puts(cloudsign_decoded_request_id(decoded));
exit_code = 0;
}
done:
if (exit_code) {
if (status == CLOUDSIGN_API_ERROR) fprintf(stderr, "HTTP %ld %s; RequestId: %s\n", cloudsign_result_http_status(result), cloudsign_result_error_code(result), cloudsign_result_request_id(result));
else fputs("Check SDK path, runtime, environment variables and business input.\n", stderr);
}
cloudsign_result_free(result);
cloudsign_decoded_free(decoded);
cloudsign_built_request_free(built);
cloudsign_client_free(client);
if (initialized) cloudsign_global_cleanup();
cloudsign_credentials_clear(&credentials);
return exit_code;
}
cmake -S . -B build
cmake --build build -j2
./build/cloudsign_api_example确认调用结果
示例正常运行后会输出请求标识。先确认程序没有抛出 SDK 或 HTTP 错误,再按下表读取成功响应,核对您实际接入的账号和资源范围。
| 字段 | 怎样理解 | 需要确认 |
|---|---|---|
request_id | 本次调用的请求标识。 | 保存用于排查;错误响应也可能有这个字段。 |
data.owner_id | 当前凭据所属的平台账号。 | 应是准备接入的本人账号。 |
data.actions | 当前身份可使用的授权动作。 | 包含下一步所需的读取或写入动作。 |
data.resources | 当前授权的应用范围。 | 覆盖准备操作的自有业务应用。 |
示例只输出请求标识时,按本语言的响应读取方式取得上述字段,不需要把完整响应或 Secret 写入日志。概念与 ID 的区别见基本概念;C 字段读取见强类型响应。
步骤四 读取自己的分发应用
身份查询成功后,在同一终端运行一次应用列表请求。为使用的凭据授予 distribution:ReadApps 和目标应用范围,使用 module=distribution 查询分发应用。
- 在自己的示例目录将下方代码保存到
example.c,并沿用步骤三的命令。 - 调用成功时取得请求 ID。集成时从
data->items读取应用列表,并保存目标应用的完整 ID。 - 列表无记录时,核对产品、实际授权和账号下的应用。继续翻页时,使用响应的
next_cursor和原筛选条件。
/* C11, trusted server. Save as example.c in the source ZIP root. */
#include <cloudsign/openapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int copy_env(char *out, size_t capacity, const char *name, int required) {
const char *value = getenv(name);
if (!value || !*value) return !required;
size_t length = strlen(value);
if (length >= capacity) return 0;
memcpy(out, value, length + 1);
return 1;
}
int main(void) {
cloudsign_credentials credentials = {0};
cloudsign_client *client = NULL;
cloudsign_result *result = NULL;
cloudsign_decoded_response *decoded = NULL;
cloudsign_built_request *built = NULL;
cloudsign_status status = CLOUDSIGN_INVALID_CONFIGURATION;
int initialized = 0, exit_code = 1;
const char *origin = getenv("CLOUDSIGN_BASE_URL");
if (!origin || !copy_env(credentials.access_key_id, sizeof(credentials.access_key_id), "CLOUDSIGN_ACCESS_KEY_ID", 1)
|| !copy_env(credentials.access_key_secret, sizeof(credentials.access_key_secret), "CLOUDSIGN_ACCESS_KEY_SECRET", 1)
|| !copy_env(credentials.security_token, sizeof(credentials.security_token), "CLOUDSIGN_SECURITY_TOKEN", 0)) goto done;
status = cloudsign_global_init();
if (status != CLOUDSIGN_OK) goto done;
initialized = 1;
cloudsign_options options = {.origin = origin, .credentials = &credentials, .timeout_ms = 15000};
status = cloudsign_client_create(&options, &client);
cloudsign_credentials_clear(&credentials);
if (status != CLOUDSIGN_OK) goto done;
const cloudsign_input_list_apps input = {.presence_module = CLOUDSIGN_FIELD_VALUE, .module = CLOUDSIGN_INPUT_LIST_APPS_MODULE_DISTRIBUTION, .presence_limit = CLOUDSIGN_FIELD_VALUE, .limit = INT64_C(2)};
status = cloudsign_build_list_apps(&input, &built);
if (status != CLOUDSIGN_OK) goto done;
status = cloudsign_send_built_request(client, built, &result);
if (status == CLOUDSIGN_OK) {
status = cloudsign_decode_list_apps(result, &decoded);
if (status != CLOUDSIGN_OK) goto done;
const cloudsign_model_list_apps_data *data = cloudsign_response_list_apps(decoded);
if (!data) { status = CLOUDSIGN_INVALID_RESPONSE; goto done; }
/* Read typed fields here; NULL/absent is distinct from zero/false. */
(void)data;
puts(cloudsign_decoded_request_id(decoded));
exit_code = 0;
}
done:
if (exit_code) {
if (status == CLOUDSIGN_API_ERROR) fprintf(stderr, "HTTP %ld %s; RequestId: %s\n", cloudsign_result_http_status(result), cloudsign_result_error_code(result), cloudsign_result_request_id(result));
else fputs("Check SDK path, runtime, environment variables and business input.\n", stderr);
}
cloudsign_result_free(result);
cloudsign_decoded_free(decoded);
cloudsign_built_request_free(built);
cloudsign_client_free(client);
if (initialized) cloudsign_global_cleanup();
cloudsign_credentials_clear(&credentials);
return exit_code;
}
在释放解码响应前读取 data->items 的 count 和条目。处理嵌套字段时参阅读取强类型响应。
企业签名应用使用 enterprise:ReadApps 和 module=enterprise。查看两种产品的接口示例 · 继续分页
接入业务
从企业签名或应用分发开始。按目标接口替换自己的资源 ID,依次查询、按业务条件确认、提交写入并核对结果。
网络中断时按业务步骤恢复原操作。上传、签名或付款各自查询最终状态。
需要组合多次调用时,请阅读上传、等待与下载助手;更新源码包前,请按升级前准备保存正在处理的任务。
常见问题
| 看到什么 | 怎样处理 |
|---|---|
| CMake 找不到依赖或示例目标 | 按包内 README 核对 CMake 与原生依赖,从解压根目录构建。由同一个 SDK 包提供头文件、库和示例工程。 |
| 401 或 RequestExpired | 核对网站主域名、程序时钟及整组凭据。使用 AKT 时加载同一组安全令牌。 |
| 403、404 或应用列表为空 | 确认身份响应、操作所需动作和目标应用范围。列表查询带上目标产品。 |
| 429 或写入结果未知 | 429 按 Retry-After 等待。写入超时时保留原资源 ID 和幂等键,查询原结果后继续。 |
记录操作名、HTTP 状态、错误码和请求 ID,去除 Secret、短期链接和敏感响应。按症状查找排查步骤。
原生编译与文件助手的系统支持情况以包内 README 为准。当前 Linux 环境已有验证;其他系统需核对工具链与文件接口,不应以 HTTP 示例成功推断所有文件助手都可用。
C SDK:读取强类型响应
将成功响应转换成 C 结构体,并正确处理空值、数组和内存释放。 查看操作步骤与完整示例 →
C SDK:构造强类型请求
使用 C 结构体填写路径、查询和正文,构建完成后再明确发送请求。 查看操作步骤与完整示例 →
C SDK:下载并校验安装包
使用短期下载凭证取得指定版本,核对独立保存的预期摘要后再使用文件。 查看操作步骤与完整示例 →
C SDK:传输与确认单个分片
自行调度上传时,发送一个已声明分片,并由业务 API 确认其保存结果。 查看操作步骤与完整示例 →
C SDK:恢复完整安装包上传
保存上传检查点,在网络中断或进程重启后继续原任务,并确认最终版本。 查看操作步骤与完整示例 →
C SDK:验证通知与返回回执
在 C 接收端验证通知来源,保存业务处理结果,再向平台返回回执。 查看操作步骤与完整示例 →