使用Postman访问腾讯云API3.0

2022-06-28 21:03:57 浏览数 (1)

环境准备

  • postman工具,版本:Version 9.22.2
  • 腾讯云API秘钥(查询链接:https://console.cloud.tencent.com/cam/capi)

配置

1、设置全局变量

将腾讯云API3.0 公共参数设置为全局变量。其中SecretId、SecretKey替换为腾讯云访问控制页面的API秘钥,其他参数任意填写

postman全局变量postman全局变量

2、访问腾讯云API3.0 接口

以下示例请求cam:GetSAMLProvider接口。

  • 配置请求前运行脚本

接口请求脚本配置接口请求脚本配置
代码语言:javascript复制
postman.setGlobalVariable("Timestamp", Date.parse(new Date()) / 1000);
postman.setGlobalVariable("Nonce", Date.parse(new Date()) / 1000);
Service = request.url.replace(/https:///, "").replace(/.tencentcloudapi.com/, "")

if (request.headers["x-tc-service"]) {
    Service = request.headers["x-tc-service"]
}
console.log(Service);

if (request.method == "GET") {
    payload = ""
    CanonicalQueryString = ""
}else if(request.method == "POST"){
    payload = request.data
    CanonicalQueryString = ""
}

var reg = /^http(s)?://(.*?)//
HashedRequestPayload = CryptoJS.SHA256(payload).toString(CryptoJS.enc.Hex);
CanonicalURI = "/";
HTTPRequestMethod = request.method
CanonicalHeaders = "content-type:application/jsonnhost:"   reg.exec(request.url)[2]   "n"
SignedHeaders = "content-type;host"

CanonicalRequest = ""   
    HTTPRequestMethod   'n'  
    CanonicalURI   'n'  
    CanonicalQueryString   'n'  
    CanonicalHeaders   'n'  
    SignedHeaders   'n'  
    HashedRequestPayload
  
console.log(CanonicalRequest);

Algorithm = "TC3-HMAC-SHA256";
RequestTimestamp = postman.getGlobalVariable("Timestamp")
Date1 = new Date().toISOString().split('T')[0]
CredentialScope = Date1   "/"   Service   "/"   "tc3_request"
HashedCanonicalRequest =  CryptoJS.SHA256(CanonicalRequest).toString(CryptoJS.enc.Hex)

StringToSign =
    Algorithm   "n"  
    RequestTimestamp   "n"  
    CredentialScope   "n"  
    HashedCanonicalRequest
    
console.log(StringToSign);

// SecretKey = ""
SecretKey = postman.getGlobalVariable("SecretKey")
SecretDate = CryptoJS.HmacSHA256(Date1, "TC3"   SecretKey)
// console.log(SecretDate)
SecretService = CryptoJS.HmacSHA256(Service, SecretDate)
SecretSigning = CryptoJS.HmacSHA256("tc3_request", SecretService)

Signature = CryptoJS.HmacSHA256(StringToSign, SecretSigning).toString(CryptoJS.enc.Hex)
postman.setGlobalVariable("Signature", Signature);

Authorization =
    Algorithm   ' '  
    'Credential='   postman.getGlobalVariable("SecretId")   '/'   CredentialScope   ', '  
    'SignedHeaders='   SignedHeaders   ', '   
    'Signature='   Signature
    
postman.setGlobalVariable("Authorization", Authorization);    

console.log(Authorization); 
  • 配置公共参数

接口公共参数接口公共参数

时间戳、签名信息请填写变量

  • 填写业务参数,发送请求

接口访问结果接口访问结果

备注:其他请求,只需要复制上述配置好的接口然后修改响应的参数。

0 人点赞