const https = require('https')const crypto = require('crypto')// 应用 ApiAppKeyconst apiAppKey = 'APIDLIA6tMfqsinsadaaaaaaaapHLkQ1z0kO5n5P'// 应用 ApiAppSecretconst apiAppSecret = 'Dc44ACV2Da3Gm9JVaaaaaaaaumYRI4CZfVG8Qiuv'const dateTime = new Date().toUTCString()const body = {arg1: 'a',arg2: 'b',}const md5 = crypto.createHash('md5').update(JSON.stringify(body), 'utf8').digest('hex')const contentMD5 = Buffer.from(md5).toString('base64')const options = {hostname: 'service-xxxxxxxx-1234567890.gz.apigw.tencentcs.com',port: 443,path: '/data',method: 'POST',headers: {Accept: 'application/json','Content-Type': 'application/json','Content-MD5': contentMD5,'Content-Length': JSON.stringify(body).length,'x-date': dateTime,},}const signingStr = [`x-date: ${dateTime}`,options.method,options.headers.Accept,options.headers['Content-Type'],contentMD5,options.path,].join('\\n')const signing = crypto.createHmac('sha1', apiAppSecret).update(signingStr, 'utf8').digest('base64')const sign = `hmac id="${apiAppKey}", algorithm="hmac-sha1", headers="x-date", signature="${signing}"`options.headers.Authorization = signconst req = https.request(options, (res) => {console.log(`STATUS: ${res.statusCode}`)res.on('data', (chunk) => {console.log('BODY: ' + chunk)})})req.on('error', (error) => {console.error(error)})req.write(JSON.stringify(body))req.end()
const https = require('https')const crypto = require('crypto')const querystring = require('querystring')const url = require('url')// 应用 ApiAppKeyconst apiAppKey = 'APIDLIA6tMfqsinsadaaaaaaaapHLkQ1z0kO5n5P'// 应用 ApiAppSecretconst apiAppSecret = 'Dc44ACV2Da3Gm9JVaaaaaaaaumYRI4CZfVG8Qiuv'const dateTime = new Date().toUTCString()const body = {arg1: 'a',arg2: 'b',}const contentMD5 = ''const options = {hostname: 'service-xxxxxxxx-1234567890.gz.apigw.tencentcs.com',port: 443,path: '/data',method: 'POST',headers: {Accept: 'application/json','Content-Type': 'application/x-www-form-urlencoded','x-date': dateTime,},}// form 参数拼接 query 参数并按照字典排序const parsedPath = url.parse(options.path, true)const sortedQueryParams = sortQueryParams({ ...body, ...parsedPath.query })const signingStr = buildSignStr(sortedQueryParams)const signing = crypto.createHmac('sha1', apiAppSecret).update(signingStr, 'utf8').digest('base64')const sign = `hmac id="${apiAppKey}", algorithm="hmac-sha1", headers="x-date", signature="${signing}"`options.headers.Authorization = sign// 发送请求const req = https.request(options, (res) => {console.log(`STATUS: ${res.statusCode}`)res.on('data', (chunk) => {console.log('BODY: ' + chunk)})})req.on('error', (error) => {console.error(error)})req.write(querystring.stringify(body))req.end()function sortQueryParams(body) {const keys = Object.keys(body).sort()let signKeys = []for (let i = 0; i < keys.length; i++) {signKeys.push(keys[i])}// 按字典序排序return signKeys.sort()}function buildSignStr(sorted_body) {const keyStr = sorted_body.map((item) => {return `${item}=${body[item]}`}).join('&')return [`x-date: ${dateTime}`,options.method,options.headers.Accept,options.headers['Content-Type'],contentMD5,`${parsedPath.pathname}${keyStr ? `?${keyStr}` : ''}`,].join('\\n')}
文档反馈