跳转至

配置域名证书

1. 接口描述

接口请求路径:POST /prod-api/acdn/tcdn/domainRest/updateDomainConfig

控制台「配置证书」按证书来源走不同接口。账号需具备 tcdn:EditDomain 权限;新上传证书时还需要 tcdn:CreateCertificate。

Token 获取方式见 密钥鉴权。

证书来源 控制台取值 调用的接口 说明
新上传证书 source = 1 先 导入证书(addCertificate),再本接口(updateDomainConfig) 把 PEM 证书和私钥导入证书库,再用返回的 data.relatesId 绑定到域名并开启 HTTPS
已托管证书 source = 2 只调用本接口(updateDomainConfig) 不导入。先用 通过域名获取可用证书 选出证书,把 relatesId 写入本接口完成绑定

注意:

  • source = 1 时,addCertificate 只负责入库,不会自动给域名开启 HTTPS。必须再用本接口把 https.switch 设为 on,并把 https.certInfo.certId 设为导入返回的 data.relatesId。
  • source = 2 时不再调用 addCertificate,只调用本接口。
  • https.certInfo.certId 是上游证书 ID,对应 relatesId,不是平台证书 ID certId。
  • https 是复杂对象。请先按域名调用 查询域名详细配置,取该域名当前的 https,再改 switch 和 certInfo.certId。未传的 HTTPS 属性会按默认值覆盖。
  • 关闭 HTTPS 时把 https.switch 设为 off,见 更新域名配置 的关闭示例。

2. 输入参数

以下请求参数列表仅列出了接口请求参数和部分公共参数,完整公共参数列表见 公共请求参数。

参数名称 必选 类型 描述
request 是 Object 待更新的域名配置
request.domain 是 String 加速域名,须已接入且属于当前账号。示例值:www.example.com
request.https 是 Https 该域名当前 HTTPS 配置。switch 设为 on
request.https.certInfo 是 ServerCert 要绑定的托管证书
request.https.certInfo.certId 是 String 上游证书 ID。新上传时取 导入证书 的 data.relatesId;已托管时取 通过域名获取可用证书 的 data[].relatesId。示例值:cert-example

3. 输出参数

成功时没有 data。

参数名称 类型 描述
code Integer 状态码。示例值:200
msg String 提示信息。示例值:成功

4. 示例

示例1 新上传证书(source = 1)

对应控制台选择「新上传证书」。先调用 导入证书(addCertificate),拿到 data.relatesId 后再调用本接口(updateDomainConfig)。下面假设导入返回的 data.relatesId 为 cert-example。https 的其余字段请替换为该域名当前配置。

输入示例

{
  "request": {
    "domain": "www.example.com",
    "https": {
      "switch": "on",
      "http2": "off",
      "ocspStapling": "off",
      "tlsVersion": ["TLSv1.2", "TLSv1.3"],
      "certInfo": {
        "certId": "cert-example"
      },
      "sslStatus": "closed",
      "hsts": {
        "switch": "off",
        "maxAge": 0,
        "includeSubDomains": "off"
      }
    }
  }
}

代码调用

请将 {登录域名}、token 替换为实际值。

curl -X POST 'https://{登录域名}/prod-api/acdn/tcdn/domainRest/updateDomainConfig' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.xxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
  "request": {
    "domain": "www.example.com",
    "https": {
      "switch": "on",
      "http2": "off",
      "ocspStapling": "off",
      "tlsVersion": ["TLSv1.2", "TLSv1.3"],
      "certInfo": {
        "certId": "cert-example"
      },
      "sslStatus": "closed",
      "hsts": {
        "switch": "off",
        "maxAge": 0,
        "includeSubDomains": "off"
      }
    }
  }
}'
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class BindDomainCertificateTest {

    public static void main(String[] args) throws Exception {
        String url = "https://{登录域名}/prod-api/acdn/tcdn/domainRest/updateDomainConfig";
        String token = "eyJhbGciOiJIUzUxMiJ9.xxxxxx";

        String requestBody = "{"
                + "\"request\":{"
                + "\"domain\":\"www.example.com\","
                + "\"https\":{"
                + "\"switch\":\"on\","
                + "\"http2\":\"off\","
                + "\"ocspStapling\":\"off\","
                + "\"tlsVersion\":[\"TLSv1.2\",\"TLSv1.3\"],"
                + "\"certInfo\":{\"certId\":\"cert-example\"},"
                + "\"sslStatus\":\"closed\","
                + "\"hsts\":{\"switch\":\"off\",\"maxAge\":0,\"includeSubDomains\":\"off\"}"
                + "}"
                + "}"
                + "}";

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Authorization", "Bearer " + token);
            httpPost.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));

            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                int statusCode = response.getStatusLine().getStatusCode();
                String body = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println("Status Code: " + statusCode);
                System.out.println("Body: " + body);
            }
        }
    }
}
package main

import (
    "bytes"
    "fmt"
    "io"
    "net/http"
)

func main() {
    url := "https://{登录域名}/prod-api/acdn/tcdn/domainRest/updateDomainConfig"
    token := "eyJhbGciOiJIUzUxMiJ9.xxxxxx"

    requestBody := []byte(`{
  "request": {
    "domain": "www.example.com",
    "https": {
      "switch": "on",
      "http2": "off",
      "ocspStapling": "off",
      "tlsVersion": ["TLSv1.2", "TLSv1.3"],
      "certInfo": {
        "certId": "cert-example"
      },
      "sslStatus": "closed",
      "hsts": {
        "switch": "off",
        "maxAge": 0,
        "includeSubDomains": "off"
      }
    }
  }
}`)

    req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(requestBody))
    if err != nil {
        panic(err)
    }
    req.Header.Set("Authorization", "Bearer "+token)
    req.Header.Set("Content-Type", "application/json")

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    body, err := io.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }
    fmt.Println("Status Code:", resp.StatusCode)
    fmt.Println("Body:", string(body))
}

输出示例

{
  "msg": "成功",
  "code": 200
}

示例2 已托管证书(source = 2)

对应控制台选择「已托管证书」。不调用 addCertificate,只调用本接口(updateDomainConfig)。certInfo.certId 使用 通过域名获取可用证书 返回的 relatesId。请求体与示例1 相同,只替换 domain 和 certId。

输入示例

{
  "request": {
    "domain": "www.example.com",
    "https": {
      "switch": "on",
      "http2": "off",
      "ocspStapling": "off",
      "tlsVersion": ["TLSv1.2", "TLSv1.3"],
      "certInfo": {
        "certId": "cert-hosted"
      },
      "sslStatus": "closed",
      "hsts": {
        "switch": "off",
        "maxAge": 0,
        "includeSubDomains": "off"
      }
    }
  }
}

代码调用

请将 {登录域名}、token 替换为实际值。

curl -X POST 'https://{登录域名}/prod-api/acdn/tcdn/domainRest/updateDomainConfig' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.xxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
  "request": {
    "domain": "www.example.com",
    "https": {
      "switch": "on",
      "http2": "off",
      "ocspStapling": "off",
      "tlsVersion": ["TLSv1.2", "TLSv1.3"],
      "certInfo": {
        "certId": "cert-hosted"
      },
      "sslStatus": "closed",
      "hsts": {
        "switch": "off",
        "maxAge": 0,
        "includeSubDomains": "off"
      }
    }
  }
}'
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class BindHostedCertificateTest {

    public static void main(String[] args) throws Exception {
        String url = "https://{登录域名}/prod-api/acdn/tcdn/domainRest/updateDomainConfig";
        String token = "eyJhbGciOiJIUzUxMiJ9.xxxxxx";

        String requestBody = "{"
                + "\"request\":{"
                + "\"domain\":\"www.example.com\","
                + "\"https\":{"
                + "\"switch\":\"on\","
                + "\"http2\":\"off\","
                + "\"ocspStapling\":\"off\","
                + "\"tlsVersion\":[\"TLSv1.2\",\"TLSv1.3\"],"
                + "\"certInfo\":{\"certId\":\"cert-hosted\"},"
                + "\"sslStatus\":\"closed\","
                + "\"hsts\":{\"switch\":\"off\",\"maxAge\":0,\"includeSubDomains\":\"off\"}"
                + "}"
                + "}"
                + "}";

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Authorization", "Bearer " + token);
            httpPost.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));

            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                int statusCode = response.getStatusLine().getStatusCode();
                String body = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println("Status Code: " + statusCode);
                System.out.println("Body: " + body);
            }
        }
    }
}
package main

import (
    "bytes"
    "fmt"
    "io"
    "net/http"
)

func main() {
    url := "https://{登录域名}/prod-api/acdn/tcdn/domainRest/updateDomainConfig"
    token := "eyJhbGciOiJIUzUxMiJ9.xxxxxx"

    requestBody := []byte(`{
  "request": {
    "domain": "www.example.com",
    "https": {
      "switch": "on",
      "http2": "off",
      "ocspStapling": "off",
      "tlsVersion": ["TLSv1.2", "TLSv1.3"],
      "certInfo": {
        "certId": "cert-hosted"
      },
      "sslStatus": "closed",
      "hsts": {
        "switch": "off",
        "maxAge": 0,
        "includeSubDomains": "off"
      }
    }
  }
}`)

    req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(requestBody))
    if err != nil {
        panic(err)
    }
    req.Header.Set("Authorization", "Bearer "+token)
    req.Header.Set("Content-Type", "application/json")

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    body, err := io.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }
    fmt.Println("Status Code:", resp.StatusCode)
    fmt.Println("Body:", string(body))
}

输出示例

{
  "msg": "成功",
  "code": 200
}

5. 错误码

上游或校验失败时,code 为 500,原因在 msg。导入证书已经成功时,证书仍留在证书库,只是没有绑到该域名。

HTTP 状态码 / 业务提示 描述
200 HTTPS 配置已提交
401 未鉴权或 Token 无效
403 无权限,需具备 tcdn:EditDomain
参数不能为空 未传请求体,或未传 request
域名不能为空 request.domain 为空
域名不属于你 域名未接入当前账号,或不在当前账号可访问的标签范围内
未初始化TCDN配置,请先联系管理员 当前账号未配置 TCDN
未初始化TCDN配置(apiKeyId),请先联系管理员 未配置 API 密钥
未初始化TCDN配置(project),请先联系管理员 未配置项目
未查询到对应的API密钥信息 密钥配置不存在
error code:{0},message:{1} 上游更新失败。{0} 为错误码,{1} 为原因