tencent cloud



最后更新时间:2021-07-26 15:43:43

    Tencent Cloud API authenticates each access request, i.e. each request needs to include authentication information (Signature) in the common parameters to verify the identity of the requester.
    The Signature is generated by the security credentials which include SecretId and SecretKey. If you don't have the security credentials yet, go to the TencentCloud API Key page to apply for them; otherwise, you cannot invoke the TencentCloud API.

    1. Applying for Security Credentials

    Before using the TencentCloud API for the first time, go to the TencentCloud API Key page to apply for security credentials.
    Security credentials consist of SecretId and SecretKey:

    • SecretId is used to identify the API requester.
    • SecretKey is used to encrypt the signature string and verify it on the server.
    • You must keep your security credentials private and avoid disclosure.

    You can apply for the security credentials through the following steps:

    1. Log in to the Tencent Cloud Console.
    2. Go to the TencentCloud API Key page.
    3. On the API Key Management page, click Create Key to create a SecretId/SecretKey pair.

    Note: Each account can have up to two pairs of SecretId/SecretKey.

    2. Generating a Signature

    With the SecretId and SecretKey, a signature can be generated. The following describes how to generate a signature:

    Assume that the SecretId and SecretKey are:

    • SecretId: AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******
    • SecretKey: Gu5t9xGARNpq86cd98joQYCN3*******

    Note: This is just an example. For actual operations, please use your own SecretId and SecretKey.

    Take the Cloud Virtual Machine's request to view the instance list (DescribeInstances) as an example. When you invoke this API, the request parameters may be as follows:

    Parameter name Description Parameter value
    Action Method name DescribeInstances
    SecretId Key ID AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******
    Timestamp Current timestamp 1465185768
    Nonce Random positive integer 11886
    Region Region where the instance is located ap-guangzhou
    InstanceIds.0 ID of the instance to query ins-09dx96dg
    Offset Offset 0
    Limit Allowed maximum output 20
    Version API version number 2017-03-12

    2.1. Sorting Parameters

    First, sort all the request parameters in an ascending lexicographical order (ASCII code) by their names. Notes: (1) Parameters are sorted by their names instead of their values; (2) The parameters are sorted based on ASCII code, not in an alphabetical order or by values. For example, InstanceIds.2 should be arranged after InstanceIds.12. You can complete the sorting process using a sorting function in a programming language, such as the ksort function in PHP. The parameters in the example are sorted as follows:

      'Action' : 'DescribeInstances',
      'InstanceIds.0' : 'ins-09dx96dg',
      'Limit' : 20,
      'Nonce' : 11886,
      'Offset' : 0,
      'Region' : 'ap-guangzhou',
      'SecretId' : 'AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******',
      'Timestamp' : 1465185768,
      'Version': '2017-03-12',

    When developing in another programming language, you can sort these sample parameters and it will work as long as you obtain the same results.

    2.2. Concatenating a Request String

    This step generates a request string.
    Format the request parameters sorted in the previous step into the form of "parameter name"="parameter value". For example, for the Action parameter, its parameter name is "Action" and its parameter value is "DescribeInstances", so it will become Action=DescribeInstances after formatted.
    Note: The "parameter value" is the original value but not the value after URL encoding.

    Then, concatenate the formatted parameters with "&". The resulting request string is as follows:


    2.3. Concatenating the Signature Original String

    This step generates a signature original string.
    The signature original string consists of the following parameters:

    1. HTTP method: POST and GET modes are supported, and GET is used here for the request. Please note that the method name should be in all capital letters.
    2. Request server: the domain name of the request to view the list of instances (DescribeInstances) is cvm.tencentcloudapi.com. The actual request domain name varies by the module to which the API belongs. For more information, see the instructions of the specific API.
    3. Request path: The request path in the current version of TencentCloud API is fixed to /.
    4. Request string: the request string generated in the previous step.

    The concatenation rule of the signature original string is: Request method + request host + request path + ? + request string

    The concatenation result of the example is:


    2.4. Generating a Signature String

    This step generates a signature string.
    First, use the HMAC-SHA1 algorithm to sign the signature original string obtained in the previous step, and then encode the generated signature using Base64 to obtain the final signature.

    The specific code is as follows with the PHP language being used as an example:

    $secretKey = 'Gu5t9xGARNpq86cd98joQYCN3*******';
    $srcStr = 'GETcvm.tencentcloudapi.com/?Action=DescribeInstances&InstanceIds.0=ins-09dx96dg&Limit=20&Nonce=11886&Offset=0&Region=ap-guangzhou&SecretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******&Timestamp=1465185768&Version=2017-03-12';
    $signStr = base64_encode(hash_hmac('sha1', $srcStr, $secretKey, true));
    echo $signStr;

    The final signature is:


    When developing in another programming language, you can sign and verify the original in the example above and it works as long as you get the same results.

    3. Encoding a Signature String

    The generated signature string cannot be directly used as a request parameter and must be URL encoded.

    For example, if the signature string generated in the previous step is zmmjn35mikh6pM3V7sUEuX4wyYM=, the final signature string request parameter (Signature) is zmmjn35mikh6pM3V7sUEuX4wyYM%3D, which will be used to generate the final request URL.

    Note: If your request method is GET, or the request method is POST and the Content-Type is application/x-www-form-urlencoded, then all the request parameter values need to be URL encoded (except the parameter key and the symbol of =) when sending the request. Non-ASCII characters need to be encoded with UTF-8 before URL encoding.

    Note: The network libraries of some programming languages automatically URL encode all parameters, in which case there is no need to URL encode the signature string; otherwise, two rounds of URL encoding will cause the signature to fail.

    Note: Other parameter values also need to be encoded using RFC 3986. Use %XY in percent-encoding for special characters such as Chinese characters, where "X" and "Y" are hexadecimal characters (0-9 and uppercase A-F), and using lowercase will cause an error.

    4. Signature Failure

    The following situational error codes for signature failure may occur. Please resolve the errors accordingly.

    Error code Error description
    AuthFailure.SignatureExpire The signature is expired
    AuthFailure.SecretIdNotFound The key does not exist
    AuthFailure.SignatureFailure Signature error
    AuthFailure.TokenFailure Token error
    AuthFailure.InvalidSecretId Invalid key (not a TencentCloud API key type)

    5. Signature Demo

    When calling API 3.0, you are recommended to use the corresponding Tencent Cloud SDK 3.0 which encapsulates the signature process, enabling you to focus on only the specific APIs provided by the product when developing. See SDK Center for more information. Currently, the following programming languages are supported:

    To further explain the signing process, we will use a programming language to implement the process described above. The request domain name, API and parameter values in the sample are used here. This goal of this example is only to provide additional clarification for the signature process, please see the SDK for actual usage.

    The final output URL might be: https://cvm.tencentcloudapi.com/?Action=DescribeInstances&InstanceIds.0=ins-09dx96dg&Limit=20&Nonce=11886&Offset=0&Region=ap-guangzhou&SecretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******&Signature=zmmjn35mikh6pM3V7sUEuX4wyYM%3D&Timestamp=1465185768&Version=2017-03-12.

    Note: The key in the example is fictitious, and the timestamp is not the current time of the system, so if this URL is opened in the browser or called using commands such as curl, an authentication error will be returned: Signature expired. In order to get a URL that can work properly, you need to replace the SecretId and SecretKey in the example with your real credentials and use the current time of the system as the Timestamp.

    Note: In the example below, even if you use the same programming language, the order of the parameters in the URL may be different for each execution. However, the order does not matter, as long as all the parameters are included in the URL and the signature is calculated correctly.

    Note: The following code is only applicable to API 3.0. It cannot be directly used in other signature processes. Even with an older API, signature calculation errors may occur due to the differences in details. Please refer to the corresponding documentation.


    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    import java.util.Random;
    import java.util.TreeMap;
    import javax.crypto.Mac;
    import javax.crypto.spec.SecretKeySpec;
    import javax.xml.bind.DatatypeConverter;
    public class TencentCloudAPIDemo {
      private final static String CHARSET = "UTF-8";
       public static String sign(String s, String key, String method) throws Exception {
          Mac mac = Mac.getInstance(method);
          SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(CHARSET), mac.getAlgorithm());
          byte[] hash = mac.doFinal(s.getBytes(CHARSET));
          return DatatypeConverter.printBase64Binary(hash);
       public static String getStringToSign(TreeMap<String, Object> params) {
          StringBuilder s2s = new StringBuilder("GETcvm.tencentcloudapi.com/?");
          // When signing, the parameters need to be sorted in lexicographical order. TreeMap is used here to guarantee the correct order.
          for (String k : params.keySet()) {
          return s2s.toString().substring(0, s2s.length() - 1);
       public static String getUrl(TreeMap<String, Object> params) throws UnsupportedEncodingException {
          StringBuilder url = new StringBuilder("https://cvm.tencentcloudapi.com/?");
          // There is no requirement for the order of the parameters in the actual request URL.
          for (String k : params.keySet()) {
              // The request string needs to be URL encoded. As the Key is all in English letters, only the value is URL encoded here.
              url.append(k).append("=").append(URLEncoder.encode(params.get(k).toString(), CHARSET)).append("&");
          return url.toString().substring(0, url.length() - 1);
       public static void main(String[] args) throws Exception {
          TreeMap<String, Object> params = new TreeMap<String, Object>(); // TreeMap enables automatic sorting
          // A random number should be used when actually calling, for example: params.put("Nonce", new Random().nextInt(java.lang.Integer.MAX_VALUE));
          params.put("Nonce", 11886); // Common parameter
          // The current time of the system should be used when actually calling, for example: params.put("Timestamp", System.currentTimeMillis() / 1000);
          params.put("Timestamp", 1465185768); // Common parameter
          params.put("SecretId", "AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******"); // Common parameter
          params.put("Action", "DescribeInstances"); // Common parameter
          params.put("Version", "2017-03-12"); // Common parameter
          params.put("Region", "ap-guangzhou"); // Common parameter
          params.put("Limit", 20); // Business parameter
          params.put("Offset", 0); // Business parameter
          params.put("InstanceIds.0", "ins-09dx96dg"); // Business parameter
          params.put("Signature", sign(getStringToSign(params), "Gu5t9xGARNpq86cd98joQYCN3*******", "HmacSHA1")); // Common parameter


    Note: If running in a Python 2 environment, the following requests dependency package must be installed first: pip install requests.

    # -*- coding: utf8 -*-
    import base64
    import hashlib
    import hmac
    import time
    import requests
    secret_id = "AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******"
    secret_key = "Gu5t9xGARNpq86cd98joQYCN3*******"
    def get_string_to_sign(method, endpoint, params):
      s = method + endpoint + "/?"
      query_str = "&".join("%s=%s" % (k, params[k]) for k in sorted(params))
      return s + query_str
    def sign_str(key, s, method):
      hmac_str = hmac.new(key.encode("utf8"), s.encode("utf8"), method).digest()
      return base64.b64encode(hmac_str)
    if __name__ == '__main__':
      endpoint = "cvm.tencentcloudapi.com"
      data = {
          'Action' : 'DescribeInstances',
          'InstanceIds.0' : 'ins-09dx96dg',
          'Limit' : 20,
          'Nonce' : 11886,
          'Offset' : 0,
          'Region' : 'ap-guangzhou',
          'SecretId' : secret_id,
          'Timestamp' : 1465185768, # int(time.time())
          'Version': '2017-03-12'
      s = get_string_to_sign("GET", endpoint, data)
      data["Signature"] = sign_str(secret_key, s, hashlib.sha1)
      # An actual invocation would occur here, which may incur fees after success
      # resp = requests.get("https://" + endpoint, params=data)
      # print(resp.url)


    package main
    import (
    func main() {
      secretId := "AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******"
      secretKey := "Gu5t9xGARNpq86cd98joQYCN3*******"
      params := map[string]string{
          "Nonce":         "11886",
          "Timestamp":     "1465185768",
          "Region":        "ap-guangzhou",
          "SecretId":      secretId,
          "Version":       "2017-03-12",
          "Action":        "DescribeInstances",
          "InstanceIds.0": "ins-09dx96dg",
          "Limit":         "20",
          "Offset":        "0",
       var buf bytes.Buffer
       // sort keys by ascii asc order
      keys := make([]string, 0, len(params))
      for k, _ := range params {
          keys = append(keys, k)
       for i := range keys {
          k := keys[i]
      buf.Truncate(buf.Len() - 1)
       hashed := hmac.New(sha1.New, []byte(secretKey))


    $secretId = "AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******";
    $secretKey = "Gu5t9xGARNpq86cd98joQYCN3*******";
    $param["Nonce"] = 11886;//rand();
    $param["Timestamp"] = 1465185768;//time();
    $param["Region"] = "ap-guangzhou";
    $param["SecretId"] = $secretId;
    $param["Version"] = "2017-03-12";
    $param["Action"] = "DescribeInstances";
    $param["InstanceIds.0"] = "ins-09dx96dg";
    $param["Limit"] = 20;
    $param["Offset"] = 0;
    $signStr = "GETcvm.tencentcloudapi.com/?";
    foreach ( $param as $key => $value ) {
      $signStr = $signStr . $key . "=" . $value . "&";
    $signStr = substr($signStr, 0, -1);
    $signature = base64_encode(hash_hmac("sha1", $signStr, $secretKey, true));
    echo $signature.PHP_EOL;
    // need to install and enable curl extension in php.ini
    // $param["Signature"] = $signature;
    // $url = "https://cvm.tencentcloudapi.com/?".http_build_query($param);
    // echo $url.PHP_EOL;
    // $ch = curl_init();
    // curl_setopt($ch, CURLOPT_URL, $url);
    // $output = curl_exec($ch);
    // curl_close($ch);
    // echo json_decode($output);


    # -*- coding: UTF-8 -*-
    # require ruby>=2.3.0
    require 'time'
    require 'openssl'
    require 'base64'
    secret_id = "AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******"
    secret_key = "Gu5t9xGARNpq86cd98joQYCN3*******"
    method = 'GET'
    endpoint = 'cvm.tencentcloudapi.com'
    data = {
    'Action' => 'DescribeInstances',
    'InstanceIds.0' => 'ins-09dx96dg',
    'Limit' => 20,
    'Nonce' => 11886,
    'Offset' => 0,
    'Region' => 'ap-guangzhou',
    'SecretId' => secret_id,
    'Timestamp' => 1465185768, # Time.now.to_i
    'Version' => '2017-03-12',
    sign = method + endpoint + '/?'
    params = []
    data.sort.each do |item|
    params << "#{item[0]}=#{item[1]}"
    sign += params.join('&')
    digest = OpenSSL::Digest.new('sha1')
    data['Signature'] = Base64.encode64(OpenSSL::HMAC.digest(digest, secret_key, sign))
    puts data['Signature']
    # require 'net/http'
    # uri = URI('https://' + endpoint)
    # uri.query = URI.encode_www_form(data)
    # p uri
    # res = Net::HTTP.get_response(uri)
    # puts res.body


    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Security.Cryptography;
    using System.Text;
    public class Application {
      public static string Sign(string signKey, string secret)
          string signRet = string.Empty;
              using (HMACSHA1 mac = new HMACSHA1(Encoding.UTF8.GetBytes(signKey)))
                  byte[] hash = mac.ComputeHash(Encoding.UTF8.GetBytes(secret));
                  signRet = Convert.ToBase64String(hash);
          return signRet;
      public static string MakeSignPlainText(SortedDictionary<string, string> requestParams, string requestMethod, string requestHost, string requestPath)
          string retStr = "";
          retStr += requestMethod;
          retStr += requestHost;
          retStr += requestPath;
          retStr += "?";
          string v = "";
          foreach (string key in requestParams.Keys)
              v += string.Format("{0}={1}&", key, requestParams[key]);
          retStr += v.TrimEnd('&');
          return retStr;
       public static void Main(string[] args)
          string SECRET_ID = "AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******";
          string SECRET_KEY = "Gu5t9xGARNpq86cd98joQYCN3*******";
           string endpoint = "cvm.tencentcloudapi.com";
          string region = "ap-guangzhou";
          string action = "DescribeInstances";
          string version = "2017-03-12";
          double RequestTimestamp = 1465185768;
          // long timestamp = ToTimestamp() / 1000;
          // string requestTimestamp = timestamp.ToString();
          Dictionary<string, string> param = new Dictionary<string, string>();
          param.Add("Limit", "20");
          param.Add("Offset", "0");
          param.Add("InstanceIds.0", "ins-09dx96dg");
          param.Add("Action", action);
          param.Add("Nonce", "11886");
          // param.Add("Nonce", Math.Abs(new Random().Next()).ToString());
           param.Add("Timestamp", RequestTimestamp.ToString());
          param.Add("Version", version);
           param.Add("SecretId", SECRET_ID);
          param.Add("Region", region);
          SortedDictionary<string, string> headers = new SortedDictionary<string, string>(param, StringComparer.Ordinal);
          string sigInParam = MakeSignPlainText(headers, "GET", endpoint, "/");
          string sigOutParam = Sign(SECRET_KEY, sigInParam);
            Console.WriteLine("GET https://cvm.tencentcloudapi.com");
          foreach (KeyValuePair<string, string> kv in headers)
              Console.WriteLine(kv.Key + ": " + kv.Value);
          Console.WriteLine("Signature" + ": " + WebUtility.UrlEncode(sigOutParam));
           string result = "https://cvm.tencentcloudapi.com/?";
          foreach (KeyValuePair<string, string> kv in headers)
              result += WebUtility.UrlEncode(kv.Key) + "=" + WebUtility.UrlEncode(kv.Value) + "&";
          result += WebUtility.UrlEncode("Signature") + "=" + WebUtility.UrlEncode(sigOutParam);
          Console.WriteLine("GET " + result);


    const crypto = require('crypto');
    function get_req_url(params, endpoint){
      params['Signature'] = escape(params['Signature']);
      const url_strParam = sort_params(params)
      return "https://" + endpoint + "/?" + url_strParam.slice(1);
    function formatSignString(reqMethod, endpoint, path, strParam){
      let strSign = reqMethod + endpoint + path + "?" + strParam.slice(1);
      return strSign;
    function sha1(secretKey, strsign){
      let signMethodMap = {'HmacSHA1': "sha1"};
      let hmac = crypto.createHmac(signMethodMap['HmacSHA1'], secretKey || "");
      return hmac.update(Buffer.from(strsign, 'utf8')).digest('base64')
    function sort_params(params){
      let strParam = "";
      let keys = Object.keys(params);
      for (let k in keys) {
          //k = k.replace(/_/g, '.');
          strParam += ("&" + keys[k] + "=" + params[keys[k]]);
      return strParam
    function main(){
      const SECRET_ID = "AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******"
      const SECRET_KEY = "Gu5t9xGARNpq86cd98joQYCN3*******"
       const endpoint = "cvm.tencentcloudapi.com"
      const Region = "ap-guangzhou"
      const Version = "2017-03-12"
      const Action = "DescribeInstances"
      const Timestamp = 1465185768
      // const Timestamp = Math.round(Date.now() / 1000)
      const Nonce = 11886
      //const nonce = Math.round(Math.random() * 65535)
       let params = {};
      params['Action'] = Action;
      params['InstanceIds.0'] = 'ins-09dx96dg';
      params['Limit'] = 20;
      params['Offset'] = 0;
      params['Nonce'] = Nonce;
      params['Region'] = Region;
      params['SecretId'] = SECRET_ID;
      params['Timestamp'] = Timestamp;
      params['Version'] = Version;
       strParam = sort_params(params)
       const reqMethod = "GET";
      const path = "/";
      strSign = formatSignString(reqMethod, endpoint, path, strParam)
       params['Signature'] = sha1(SECRET_KEY, strSign)
       const req_url = get_req_url(params, endpoint)




    7x24 电话支持