腾讯有数官方文档

后台数据上报接口

为了提高数据接入的稳定性和安全性,特提供后台数据上报接口

签名

下面是签名过程会涉及的参数

参数 说明

app_id

数据中心分配的应用ID

app_secret

数据中心分配的签名密钥,需要绝对保密

timestamp

当前时间戳,单位为秒

nonce

随机字符串标识,不超过32个字符

sign

签名算法,目前只支持 sha256

signature

最终的签名参数

计算签名

  1. 随机生成 timestampnonce

  2. 拼接字符串:app_id=<app_id>&nonce=<nonce>&sign=<sign>&timestamp=<timestamp>(key已经按字符串顺序顺次排列,不需要对value进行url encode)

  3. 使用`sign`算法和`app_secret`对拼接的字符串进行加密,然后使用hex进行编码,获得签名`signature`

示例

  1. 随机生成:timestamp = "1542951251", nonce = "407313d23c3f7"

  2. 拼接字符串:conStr = "app_id=abc&nonce=407313d23c3f7&sign=sha256&timestamp=1542951251"

  3. signature = hex(sha256(conStr)) = "6e54c38c7e9c382a5295c3885503fa4a9ee37cabc884350d84973fc1b843126d"

请求

POST https://zhls.qq.com/api/v1/safe-report?app_id=<app_id>&nonce=<nonce>&timestamp=<timestamp>&sign=<sign>&signature=<signature>
body <数据内容: JSON Object or JSON Array>

代码示例

const crypto = require('crypto')
const request = require('request')
const app_id = 'abc'
const app_secret = 'xyz'

const timestamp = parseInt(Date.now() / 1000)

const nonce = Math.random().toString(16).substring(2) // 生成随机字符串

const str = `app_id=${app_id}&nonce=${nonce}&sign=sha256&timestamp=${timestamp}`
console.log(str)

const signature = crypto.createHmac('sha256', app_secret).update(Buffer.from(str, 'utf-8')).digest('hex');
console.log(signature)

let url = `https://zhls.qq.com/api/v1/safe-report?${str}&signature=${signature}`

request({
    method: "POST",
    url: url,
    json: true,
    timeout: 1000,
    body: {
        data: 'hello world'
    }
}, function (err, rsp, body) {
    if (!err && rsp.statusCode == 200) {
        console.log('report success', JSON.stringify(body))
    } else {
        console.log(`report err`, err, (rsp || {}).statusCode)
    }
})