Globals添加SecretId和SecretKey
![测试环境请求示例](https://i-blog.csdnimg.cn/blog_migrate/b12e578ddecc1b00c9fe90d1ea79ad0a.png)
Pre-request Script添加以下内容
postman.setGlobalVariable("Timestamp", Date.parse(new Date()) / 1000);
postman.setGlobalVariable("Nonce", Date.parse(new Date()) / 1000);
Service = request.url.replace(/https:\/\//, "").replace(/\.test\.ess\.tencent\.cn\//, "")
if (request.headers["x-tc-service"]) {
Service = request.headers["x-tc-service"]
}
console.log("Service:"+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/json\nhost:" + reg.exec(request.url)[2] + "\n"
SignedHeaders = "content-type;host"
CanonicalRequest = "" +
HTTPRequestMethod + '\n' +
CanonicalURI + '\n' +
CanonicalQueryString + '\n' +
CanonicalHeaders + '\n' +
SignedHeaders + '\n' +
HashedRequestPayload
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
SecretKey = postman.getGlobalVariable("SecretKey")
console.log("SecretKey:"+SecretKey);
SecretDate = CryptoJS.HmacSHA256(Date1, "TC3" + SecretKey)
SecretService = CryptoJS.HmacSHA256(Service, SecretDate)
SecretSigning = CryptoJS.HmacSHA256("tc3_request", SecretService)
Signature = CryptoJS.HmacSHA256(StringToSign, SecretSigning).toString(CryptoJS.enc.Hex)
postman.setGlobalVariable("Signature", Signature);
SecretId = postman.getGlobalVariable("SecretId")
console.log("SecretId:"+SecretId);
Authorization =
Algorithm + ' ' +
'Credential=' + SecretId + '/' + CredentialScope + ', ' +
'SignedHeaders=' + SignedHeaders + ', ' +
'Signature=' + Signature
postman.setGlobalVariable("Authorization", Authorization);
console.log("Authorization:"+Authorization);