//获取起始时间戳
let timestamp = parseInt(new Date().getTime()/1000);
//获取截止时间戳
let timestamp2 = timestamp + 3600;
//获取method
let method = pm.request.method.toString().toLowerCase();
//获取整个url
let arrUrl = pm.request.url
//通过url获取uri
let uuu = arrUrl.toString().split('//')
let start = uuu[1].indexOf("/");
let end = uuu[1].indexOf("?");
if (end==-1){
end = uuu[1].length;
}
let uri = uuu[1].substring(start,end);//stop省略,截取从start开始到结尾的所有字符
//签名的必备参数
let qAk = pm.globals.get("SecretId");
let SecretKey = pm.globals.get("SecretKey");
//计算签名(很多项目中的签名,原始字符串还会加入请求参数)
let qSignAlgorithm = 'sha1';
let qSignTime = timestamp + ';' + timestamp2;
let qKeyTime = timestamp + ';' + timestamp2;
let signKey = CryptoJS.HmacSHA1(qKeyTime,SecretKey).toString();
// 步骤二:构成 FormatString
let formatString = [method, uri, '', '', '',].join('\n');
// 步骤三:计算 StringToSign
let stringToSign = ['sha1', qSignTime, CryptoJS.SHA1(formatString).toString(),''].join('\n');
// 步骤四:计算 Signature
let Signature = CryptoJS.HmacSHA1(stringToSign, signKey).toString();
// 步骤五:构造 Authorization
let authorization = [
'q-sign-algorithm=' + qSignAlgorithm,
'q-ak=' + qAk,
'q-sign-time=' + qSignTime,
'q-key-time=' + qKeyTime,
'q-header-list='+'',
'q-url-param-list='+'',
'q-signature=' + Signature
].join('&');
pm.environment.set("Authorization", authorization);
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。