tencent cloud

文档反馈

签名方法 v3

最后更新时间:2021-11-15 17:22:46

    签名方法 v3 (TC3-HMAC-SHA256)功能上覆盖了以前的签名方法 v1,而且更安全,支持更大的请求,支持 json 格式,性能有一定提升,推荐使用该签名方法计算签名。

    首次接触,建议使用 API Explorer 中的“签名串生成”功能,选择签名版本为“API 3.0 签名 v3”,可以生成签名过程进行验证,也可直接生成 SDK 代码。推荐使用腾讯云 API 配套的 7 种常见的编程语言 SDK,已经封装了签名和请求过程,均已开源,支持 PythonJavaPHPGoNodeJS.NETC++

    腾讯云 API 会对每个请求进行身份验证,用户需要使用安全凭证,经过特定的步骤对请求进行签名(Signature),每个请求都需要在公共请求参数中指定该签名结果并以指定的方式和格式发送请求。

    申请安全凭证

    本文使用的安全凭证为密钥,密钥包括 SecretId 和 SecretKey。每个用户最多可以拥有两对密钥。

    • SecretId:用于标识 API 调用者身份,可以简单类比为用户名。
    • SecretKey:用于验证 API 调用者的身份,可以简单类比为密码。
    • 用户必须严格保管安全凭证,避免泄露,否则将危及财产安全。如已泄漏,请立刻禁用该安全凭证。

    申请安全凭证的具体步骤如下:

    1. 登录 腾讯云管理中心控制台
    2. 前往 云API密钥 的控制台页面。
    3. 云API密钥 页面,单击【新建密钥】即可以创建一对密钥。

    签名过程

    云 API 支持 GET 和 POST 请求。对于GET方法,只支持 Content-Type: application/x-www-form-urlencoded 协议格式。对于POST方法,目前支持 Content-Type: application/json 以及 Content-Type: multipart/form-data 两种协议格式,json 格式绝大多数接口均支持,multipart 格式只有特定接口支持,此时该接口不能使用 json 格式调用,参考具体业务接口文档说明。推荐使用 POST 请求,因为两者的结果并无差异,但 GET 请求只支持 32 KB 以内的请求包。

    下面以云服务器查询广州区实例列表作为例子,分步骤介绍签名的计算过程。我们选择该接口是因为:

    1. 云服务器默认已开通,该接口很常用;
    2. 该接口是只读的,不会改变现有资源的状态;
    3. 接口覆盖的参数种类较全,可以演示包含数据结构的数组如何使用。

    在示例中,不论公共参数或者接口的参数,我们尽量选择容易犯错的情况。在实际调用接口时,请根据实际情况来,每个接口的参数并不相同,不要照抄这个例子的参数和值。

    假设用户的 SecretId 和 SecretKey 分别是:AKIDz8krbsJ5yKBZQpn74WFkmLPx3EXAMPLE 和 Gu5t9xGARNpq86cd98joQYCN3EXAMPLE。用户想查看广州区云服务器名为“未命名”的主机状态,只返回一条数据。则请求可能为:

    curl -X POST https://cvm.tencentcloudapi.com \
    -H "Authorization: TC3-HMAC-SHA256 Credential=AKIDz8krbsJ5yKBZQpn74WFkmLPx3EXAMPLE/2019-02-25/cvm/tc3_request, SignedHeaders=content-type;host, Signature=72e494ea809ad7a8c8f7a4507b9bddcbaa8e581f516e8da2f66e2c5a96525168" \
    -H "Content-Type: application/json; charset=utf-8" \
    -H "Host: cvm.tencentcloudapi.com" \
    -H "X-TC-Action: DescribeInstances" \
    -H "X-TC-Timestamp: 1551113065" \
    -H "X-TC-Version: 2017-03-12" \
    -H "X-TC-Region: ap-guangzhou" \
    -d '{"Limit": 1, "Filters": [{"Values": ["\u672a\u547d\u540d"], "Name": "instance-name"}]}'
    

    下面详细解释签名计算过程。

    1. 拼接规范请求串

    按如下伪代码格式拼接规范请求串(CanonicalRequest):

    CanonicalRequest =
      HTTPRequestMethod + '\n' +
      CanonicalURI + '\n' +
      CanonicalQueryString + '\n' +
      CanonicalHeaders + '\n' +
      SignedHeaders + '\n' +
      HashedRequestPayload
    
    字段名称 解释
    HTTPRequestMethod HTTP 请求方法(GET、POST )。此示例取值为 POST
    CanonicalURI URI 参数,API 3.0 固定为正斜杠(/)。
    CanonicalQueryString 发起 HTTP 请求 URL 中的查询字符串,对于 POST 请求,固定为空字符串"",对于 GET 请求,则为 URL 中问号(?)后面的字符串内容,例如:Limit=10&Offset=0。
    注意:CanonicalQueryString 需要参考 RFC3986 进行 URLEncode,字符集 UTF8,推荐使用编程语言标准库,所有特殊字符均需编码,大写形式。
    CanonicalHeaders 参与签名的头部信息,至少包含 host 和 content-type 两个头部,也可加入自定义的头部参与签名以提高自身请求的唯一性和安全性。
    拼接规则:
    1. 头部 key 和 value 统一转成小写,并去掉首尾空格,按照 key:value\n 格式拼接;
    2. 多个头部,按照头部 key(小写)的 ASCII 升序进行拼接。
    此示例计算结果是 content-type:application/json; charset=utf-8\nhost:cvm.tencentcloudapi.com\n
    注意:content-type 必须和实际发送的相符合,有些编程语言网络库即使未指定也会自动添加 charset 值,如果签名时和发送时不一致,服务器会返回签名校验失败。
    SignedHeaders 参与签名的头部信息,说明此次请求有哪些头部参与了签名,和 CanonicalHeaders 包含的头部内容是一一对应的。content-type 和 host 为必选头部。
    拼接规则:
    1. 头部 key 统一转成小写;
    2. 多个头部 key(小写)按照 ASCII 升序进行拼接,并且以分号(;)分隔。
    此示例为 content-type;host
    HashedRequestPayload 请求正文(payload,即 body,此示例为 {"Limit": 1, "Filters": [{"Values": ["\u672a\u547d\u540d"], "Name": "instance-name"}]})的哈希值,计算伪代码为 Lowercase(HexEncode(Hash.SHA256(RequestPayload))),即对 HTTP 请求正文做 SHA256 哈希,然后十六进制编码,最后编码串转换成小写字母。对于 GET 请求,RequestPayload 固定为空字符串。此示例计算结果是 35e9c5b0e3ae67532d3c9f17ead6c90222632e5b1ff7f6e89887f1398934f064

    根据以上规则,示例中得到的规范请求串如下:

    POST
    /
    content-type:application/json; charset=utf-8
    host:cvm.tencentcloudapi.com
    content-type;host
    35e9c5b0e3ae67532d3c9f17ead6c90222632e5b1ff7f6e89887f1398934f064
    

    2. 拼接待签名字符串

    按如下格式拼接待签名字符串:

    StringToSign =
      Algorithm + \n +
      RequestTimestamp + \n +
      CredentialScope + \n +
      HashedCanonicalRequest
    
    字段名称 解释
    Algorithm 签名算法,目前固定为 TC3-HMAC-SHA256
    RequestTimestamp 请求时间戳,即请求头部的公共参数 X-TC-Timestamp 取值,取当前时间 UNIX 时间戳,精确到秒。此示例取值为 1551113065
    CredentialScope 凭证范围,格式为 Date/service/tc3_request,包含日期、所请求的服务和终止字符串(tc3_request)。Date 为 UTC 标准时间的日期,取值需要和公共参数 X-TC-Timestamp 换算的 UTC 标准时间日期一致;service 为产品名,必须与调用的产品域名一致。此示例计算结果是 2019-02-25/cvm/tc3_request。
    HashedCanonicalRequest 前述步骤拼接所得规范请求串的哈希值,计算伪代码为 Lowercase(HexEncode(Hash.SHA256(CanonicalRequest)))。此示例计算结果是 5ffe6a04c0664d6b969fab9a13bdab201d63ee709638e2749d62a09ca18d7031

    注意:

    1. Date 必须从时间戳 X-TC-Timestamp 计算得到,且时区为 UTC+0。如果加入系统本地时区信息,例如东八区,将导致白天和晚上调用成功,但是凌晨时调用必定失败。假设时间戳为 1551113065,在东八区的时间是 2019-02-26 00:44:25,但是计算得到的 Date 取 UTC+0 的日期应为 2019-02-25,而不是 2019-02-26。
    2. Timestamp 必须是当前系统时间,且需确保系统时间和标准时间是同步的,如果相差超过五分钟则必定失败。如果长时间不和标准时间同步,可能导致运行一段时间后,请求必定失败,返回签名过期错误。

    根据以上规则,示例中得到的待签名字符串如下:

    TC3-HMAC-SHA256
    1551113065
    2019-02-25/cvm/tc3_request
    5ffe6a04c0664d6b969fab9a13bdab201d63ee709638e2749d62a09ca18d7031
    

    3. 计算签名

    1)计算派生签名密钥,伪代码如下:

    SecretKey = "Gu5t9xGARNpq86cd98joQYCN3EXAMPLE"
    SecretDate = HMAC_SHA256("TC3" + SecretKey, Date)
    SecretService = HMAC_SHA256(SecretDate, Service)
    SecretSigning = HMAC_SHA256(SecretService, "tc3_request")
    

    派生出的密钥 SecretDateSecretServiceSecretSigning 是二进制的数据,可能包含不可打印字符,此处不展示中间结果。

    请注意,不同的编程语言,HMAC 库函数中参数顺序可能不一样,此处的伪代码密钥参数在后,请以实际编程语言为准。通常标准库函数会提供二进制格式的计算值,也即此处使用的,也会提供打印友好的十六进制格式的计算值,将在下面计算签名结果时使用。

    字段名称 解释
    SecretKey 原始的 SecretKey,即 Gu5t9xGARNpq86cd98joQYCN3EXAMPLE
    Date 即 Credential 中的 Date 字段信息。此示例取值为 2019-02-25
    Service 即 Credential 中的 Service 字段信息。此示例取值为 cvm

    2)计算签名,伪代码如下:

    Signature = HexEncode(HMAC_SHA256(SecretSigning, StringToSign))
    

    此示例计算结果是 72e494ea809ad7a8c8f7a4507b9bddcbaa8e581f516e8da2f66e2c5a96525168

    4. 拼接 Authorization

    按如下格式拼接 Authorization:

    Authorization =
      Algorithm + ' ' +
      'Credential=' + SecretId + '/' + CredentialScope + ', ' +
      'SignedHeaders=' + SignedHeaders + ', ' +
      'Signature=' + Signature
    
    字段名称 解释
    Algorithm 签名方法,固定为 TC3-HMAC-SHA256
    SecretId 密钥对中的 SecretId,即 AKIDz8krbsJ5yKBZQpn74WFkmLPx3EXAMPLE
    CredentialScope 见上文,凭证范围。此示例计算结果是 2019-02-25/cvm/tc3_request
    SignedHeaders 见上文,参与签名的头部信息。此示例取值为 content-type;host
    Signature 签名值。此示例计算结果是 72e494ea809ad7a8c8f7a4507b9bddcbaa8e581f516e8da2f66e2c5a96525168

    根据以上规则,示例中得到的值为:

    TC3-HMAC-SHA256 Credential=AKIDz8krbsJ5yKBZQpn74WFkmLPx3EXAMPLE/2019-02-25/cvm/tc3_request, SignedHeaders=content-type;host, Signature=72e494ea809ad7a8c8f7a4507b9bddcbaa8e581f516e8da2f66e2c5a96525168
    

    最终完整的调用信息如下:

    POST https://cvm.tencentcloudapi.com/
    Authorization: TC3-HMAC-SHA256 Credential=AKIDz8krbsJ5yKBZQpn74WFkmLPx3EXAMPLE/2019-02-25/cvm/tc3_request, SignedHeaders=content-type;host, Signature=72e494ea809ad7a8c8f7a4507b9bddcbaa8e581f516e8da2f66e2c5a96525168
    Content-Type: application/json; charset=utf-8
    Host: cvm.tencentcloudapi.com
    X-TC-Action: DescribeInstances
    X-TC-Version: 2017-03-12
    X-TC-Timestamp: 1551113065
    X-TC-Region: ap-guangzhou
    {"Limit": 1, "Filters": [{"Values": ["\u672a\u547d\u540d"], "Name": "instance-name"}]}
    

    5. 签名演示

    Java

    import java.nio.charset.Charset;
    import java.nio.charset.StandardCharsets;
    import java.security.MessageDigest;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.TimeZone;
    import java.util.TreeMap;
    import javax.crypto.Mac;
    import javax.crypto.spec.SecretKeySpec;
    import javax.xml.bind.DatatypeConverter;
    public class TencentCloudAPITC3Demo {
      private final static Charset UTF8 = StandardCharsets.UTF_8;
      private final static String SECRET_ID = "AKIDz8krbsJ5yKBZQpn74WFkmLPx3EXAMPLE";
      private final static String SECRET_KEY = "Gu5t9xGARNpq86cd98joQYCN3EXAMPLE";
      private final static String CT_JSON = "application/json; charset=utf-8";
       public static byte[] hmac256(byte[] key, String msg) throws Exception {
          Mac mac = Mac.getInstance("HmacSHA256");
          SecretKeySpec secretKeySpec = new SecretKeySpec(key, mac.getAlgorithm());
          mac.init(secretKeySpec);
          return mac.doFinal(msg.getBytes(UTF8));
      }
       public static String sha256Hex(String s) throws Exception {
          MessageDigest md = MessageDigest.getInstance("SHA-256");
          byte[] d = md.digest(s.getBytes(UTF8));
          return DatatypeConverter.printHexBinary(d).toLowerCase();
      }
       public static void main(String[] args) throws Exception {
          String service = "cvm";
          String host = "cvm.tencentcloudapi.com";
          String region = "ap-guangzhou";
          String action = "DescribeInstances";
          String version = "2017-03-12";
          String algorithm = "TC3-HMAC-SHA256";
          String timestamp = "1551113065";
          //String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          // 注意时区,否则容易出错
          sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
          String date = sdf.format(new Date(Long.valueOf(timestamp + "000")));
           // ************* 步骤 1:拼接规范请求串 *************
          String httpRequestMethod = "POST";
          String canonicalUri = "/";
          String canonicalQueryString = "";
          String canonicalHeaders = "content-type:application/json; charset=utf-8\n" + "host:" + host + "\n";
          String signedHeaders = "content-type;host";
           String payload = "{\"Limit\": 1, \"Filters\": [{\"Values\": [\"\\u672a\\u547d\\u540d\"], \"Name\": \"instance-name\"}]}";
          String hashedRequestPayload = sha256Hex(payload);
          String canonicalRequest = httpRequestMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n"
                  + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestPayload;
          System.out.println(canonicalRequest);
           // ************* 步骤 2:拼接待签名字符串 *************
          String credentialScope = date + "/" + service + "/" + "tc3_request";
          String hashedCanonicalRequest = sha256Hex(canonicalRequest);
          String stringToSign = algorithm + "\n" + timestamp + "\n" + credentialScope + "\n" + hashedCanonicalRequest;
          System.out.println(stringToSign);
           // ************* 步骤 3:计算签名 *************
          byte[] secretDate = hmac256(("TC3" + SECRET_KEY).getBytes(UTF8), date);
          byte[] secretService = hmac256(secretDate, service);
          byte[] secretSigning = hmac256(secretService, "tc3_request");
          String signature = DatatypeConverter.printHexBinary(hmac256(secretSigning, stringToSign)).toLowerCase();
          System.out.println(signature);
           // ************* 步骤 4:拼接 Authorization *************
          String authorization = algorithm + " " + "Credential=" + SECRET_ID + "/" + credentialScope + ", "
                  + "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature;
          System.out.println(authorization);
           TreeMap<String, String> headers = new TreeMap<String, String>();
          headers.put("Authorization", authorization);
          headers.put("Content-Type", CT_JSON);
          headers.put("Host", host);
          headers.put("X-TC-Action", action);
          headers.put("X-TC-Timestamp", timestamp);
          headers.put("X-TC-Version", version);
          headers.put("X-TC-Region", region);
           StringBuilder sb = new StringBuilder();
          sb.append("curl -X POST https://").append(host)
          .append(" -H \"Authorization: ").append(authorization).append("\"")
          .append(" -H \"Content-Type: application/json; charset=utf-8\"")
          .append(" -H \"Host: ").append(host).append("\"")
          .append(" -H \"X-TC-Action: ").append(action).append("\"")
          .append(" -H \"X-TC-Timestamp: ").append(timestamp).append("\"")
          .append(" -H \"X-TC-Version: ").append(version).append("\"")
          .append(" -H \"X-TC-Region: ").append(region).append("\"")
          .append(" -d '").append(payload).append("'");
          System.out.println(sb.toString());
      }
    }
    

    Python

    # -*- coding: utf-8 -*-
    import hashlib, hmac, json, os, sys, time
    from datetime import datetime
    # 密钥参数
    secret_id = "AKIDz8krbsJ5yKBZQpn74WFkmLPx3EXAMPLE"
    secret_key = "Gu5t9xGARNpq86cd98joQYCN3EXAMPLE"
    service = "cvm"
    host = "cvm.tencentcloudapi.com"
    endpoint = "https://" + host
    region = "ap-guangzhou"
    action = "DescribeInstances"
    version = "2017-03-12"
    algorithm = "TC3-HMAC-SHA256"
    #timestamp = int(time.time())
    timestamp = 1551113065
    date = datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d")
    params = {"Limit": 1, "Filters": [{"Name": "instance-name", "Values": [u"未命名"]}]}
    # ************* 步骤 1:拼接规范请求串 *************
    http_request_method = "POST"
    canonical_uri = "/"
    canonical_querystring = ""
    ct = "application/json; charset=utf-8"
    payload = json.dumps(params)
    canonical_headers = "content-type:%s\nhost:%s\n" % (ct, host)
    signed_headers = "content-type;host"
    hashed_request_payload = hashlib.sha256(payload.encode("utf-8")).hexdigest()
    canonical_request = (http_request_method + "\n" +
                       canonical_uri + "\n" +
                       canonical_querystring + "\n" +
                       canonical_headers + "\n" +
                       signed_headers + "\n" +
                       hashed_request_payload)
    print(canonical_request)
    # ************* 步骤 2:拼接待签名字符串 *************
    credential_scope = date + "/" + service + "/" + "tc3_request"
    hashed_canonical_request = hashlib.sha256(canonical_request.encode("utf-8")).hexdigest()
    string_to_sign = (algorithm + "\n" +
                    str(timestamp) + "\n" +
                    credential_scope + "\n" +
                    hashed_canonical_request)
    print(string_to_sign)
    # ************* 步骤 3:计算签名 *************
    # 计算签名摘要函数
    def sign(key, msg):
      return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
    secret_date = sign(("TC3" + secret_key).encode("utf-8"), date)
    secret_service = sign(secret_date, service)
    secret_signing = sign(secret_service, "tc3_request")
    signature = hmac.new(secret_signing, string_to_sign.encode("utf-8"), hashlib.sha256).hexdigest()
    print(signature)
    # ************* 步骤 4:拼接 Authorization *************
    authorization = (algorithm + " " +
                   "Credential=" + secret_id + "/" + credential_scope + ", " +
                   "SignedHeaders=" + signed_headers + ", " +
                   "Signature=" + signature)
    print(authorization)
    print('curl -X POST ' + endpoint
        + ' -H "Authorization: ' + authorization + '"'
        + ' -H "Content-Type: application/json; charset=utf-8"'
        + ' -H "Host: ' + host + '"'
        + ' -H "X-TC-Action: ' + action + '"'
        + ' -H "X-TC-Timestamp: ' + str(timestamp) + '"'
        + ' -H "X-TC-Version: ' + version + '"'
        + ' -H "X-TC-Region: ' + region + '"'
        + " -d '" + payload + "'")
    

    go

    package main
    import (
      "crypto/hmac"
      "crypto/sha256"
      "encoding/hex"
      "fmt"
      "time"
    )
    func sha256hex(s string) string {
      b := sha256.Sum256([]byte(s))
      return hex.EncodeToString(b[:])
    }
    func hmacsha256(s, key string) string {
      hashed := hmac.New(sha256.New, []byte(key))
      hashed.Write([]byte(s))
      return string(hashed.Sum(nil))
    }
    func main() {
      secretId := "AKIDz8krbsJ5yKBZQpn74WFkmLPx3EXAMPLE"
      secretKey := "Gu5t9xGARNpq86cd98joQYCN3EXAMPLE"
      host := "cvm.tencentcloudapi.com"
      algorithm := "TC3-HMAC-SHA256"
      service := "cvm"
      version := "2017-03-12"
      action := "DescribeInstances"
      region := "ap-guangzhou"
      //var timestamp int64 = time.Now().Unix()
      var timestamp int64 = 1551113065
       // step 1: build canonical request string
      httpRequestMethod := "POST"
      canonicalURI := "/"
      canonicalQueryString := ""
      canonicalHeaders := "content-type:application/json; charset=utf-8\n" + "host:" + host + "\n"
      signedHeaders := "content-type;host"
      payload := `{"Limit": 1, "Filters": [{"Values": ["\u672a\u547d\u540d"], "Name": "instance-name"}]}`
      hashedRequestPayload := sha256hex(payload)
      canonicalRequest := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s",
          httpRequestMethod,
          canonicalURI,
          canonicalQueryString,
          canonicalHeaders,
          signedHeaders,
          hashedRequestPayload)
      fmt.Println(canonicalRequest)
       // step 2: build string to sign
      date := time.Unix(timestamp, 0).UTC().Format("2006-01-02")
      credentialScope := fmt.Sprintf("%s/%s/tc3_request", date, service)
      hashedCanonicalRequest := sha256hex(canonicalRequest)
      string2sign := fmt.Sprintf("%s\n%d\n%s\n%s",
          algorithm,
          timestamp,
          credentialScope,
          hashedCanonicalRequest)
      fmt.Println(string2sign)
       // step 3: sign string
      secretDate := hmacsha256(date, "TC3"+secretKey)
      secretService := hmacsha256(service, secretDate)
      secretSigning := hmacsha256("tc3_request", secretService)
      signature := hex.EncodeToString([]byte(hmacsha256(string2sign, secretSigning)))
      fmt.Println(signature)
       // step 4: build authorization
      authorization := fmt.Sprintf("%s Credential=%s/%s, SignedHeaders=%s, Signature=%s",
          algorithm,
          secretId,
          credentialScope,
          signedHeaders,
          signature)
      fmt.Println(authorization)
       curl := fmt.Sprintf(`curl -X POST https://%s\
    -H "Authorization: %s"\
    -H "Content-Type: application/json; charset=utf-8"\
    -H "Host: %s" -H "X-TC-Action: %s"\
    -H "X-TC-Timestamp: %d"\
    -H "X-TC-Version: %s"\
    -H "X-TC-Region: %s"\
    -d '%s'`, host, authorization, host, action, timestamp, version, region, payload)
      fmt.Println(curl)
    }
    

    PHP

    <?php
    $secretId = "AKIDz8krbsJ5yKBZQpn74WFkmLPx3EXAMPLE";
    $secretKey = "Gu5t9xGARNpq86cd98joQYCN3EXAMPLE";
    $host = "cvm.tencentcloudapi.com";
    $service = "cvm";
    $version = "2017-03-12";
    $action = "DescribeInstances";
    $region = "ap-guangzhou";
    // $timestamp = time();
    $timestamp = 1551113065;
    $algorithm = "TC3-HMAC-SHA256";
    // step 1: build canonical request string
    $httpRequestMethod = "POST";
    $canonicalUri = "/";
    $canonicalQueryString = "";
    $canonicalHeaders = "content-type:application/json; charset=utf-8\n"."host:".$host."\n";
    $signedHeaders = "content-type;host";
    $payload = '{"Limit": 1, "Filters": [{"Values": ["\u672a\u547d\u540d"], "Name": "instance-name"}]}';
    $hashedRequestPayload = hash("SHA256", $payload);
    $canonicalRequest = $httpRequestMethod."\n"
      .$canonicalUri."\n"
      .$canonicalQueryString."\n"
      .$canonicalHeaders."\n"
      .$signedHeaders."\n"
      .$hashedRequestPayload;
    echo $canonicalRequest.PHP_EOL;
    // step 2: build string to sign
    $date = gmdate("Y-m-d", $timestamp);
    $credentialScope = $date."/".$service."/tc3_request";
    $hashedCanonicalRequest = hash("SHA256", $canonicalRequest);
    $stringToSign = $algorithm."\n"
      .$timestamp."\n"
      .$credentialScope."\n"
      .$hashedCanonicalRequest;
    echo $stringToSign.PHP_EOL;
    // step 3: sign string
    $secretDate = hash_hmac("SHA256", $date, "TC3".$secretKey, true);
    $secretService = hash_hmac("SHA256", $service, $secretDate, true);
    $secretSigning = hash_hmac("SHA256", "tc3_request", $secretService, true);
    $signature = hash_hmac("SHA256", $stringToSign, $secretSigning);
    echo $signature.PHP_EOL;
    // step 4: build authorization
    $authorization = $algorithm
      ." Credential=".$secretId."/".$credentialScope
      .", SignedHeaders=content-type;host, Signature=".$signature;
    echo $authorization.PHP_EOL;
    $curl = "curl -X POST https://".$host
      .' -H "Authorization: '.$authorization.'"'
      .' -H "Content-Type: application/json; charset=utf-8"'
      .' -H "Host: '.$host.'"'
      .' -H "X-TC-Action: '.$action.'"'
      .' -H "X-TC-Timestamp: '.$timestamp.'"'
      .' -H "X-TC-Version: '.$version.'"'
      .' -H "X-TC-Region: '.$region.'"'
      ." -d '".$payload."'";
    echo $curl.PHP_EOL;
    

    签名失败

    存在以下签名失败的错误码,请根据实际情况处理。

    错误码 错误描述
    AuthFailure.SignatureExpire 签名过期。Timestamp 与服务器接收到请求的时间相差不得超过五分钟。
    AuthFailure.SecretIdNotFound 密钥不存在。请到控制台查看密钥是否被禁用,是否少复制了字符或者多了字符。
    AuthFailure.SignatureFailure 签名错误。可能是签名计算错误,或者签名与实际发送的内容不相符合,也有可能是密钥 SecretKey 错误导致的。
    AuthFailure.TokenFailure 临时证书 Token 错误。
    AuthFailure.InvalidSecretId 密钥非法(不是云 API 密钥类型)。
    联系我们

    联系我们,为您的业务提供专属服务。

    技术支持

    如果你想寻求进一步的帮助,通过工单与我们进行联络。我们提供7x24的工单服务。

    7x24 电话支持