以下是我如何生成服务器请求的选项:
function multipartFormData(params, url, key, nonce, signature) {
var boundary = '----WebKitFormBoundary' + nonce;
var bodyString = [];
bodyString.push(
'--' + boundary,
'Content-Disposition: form-data; name="document"; filename="image.png"',
'Content-Type: image/png',
'',
fs.createReadStream('image/image.png')
);
bodyString.push('--' + boundary + '--','');
var content = bodyString.join('\r\n');
return {
formData: content,
url: url + '/',
headers: {
'Content-Type': 'multipart/form-data; boundary=' + boundary,
'Apiauth-Key': key,
'Apiauth-Nonce': nonce,
'Apiauth-Signature': signature,
'Content-Length': content.length
}
}
}
有签名哈希身份验证密钥:
function getMessageSignature(path, params, nonce) {
var data_params = params;
var boundary = '----WebKitFormBoundary' + nonce;
var postParameters = querystring.stringify(data_params);
var path = '/api' + path + '/';
let message = nonce + config.key + path + 'POSTmultipart/form-data; boundary=' + boundary + 'image.png';
var auth_hash = crypto.createHmac("sha256", config.secret).update(message).digest('hex').toUpperCase();
return auth_hash;
}
所以我随即提出要求:
function Client(key, secret) {
var nonce = new Date() * 1000;
var config = {
url: 'https://somesite/api',
key: key,
secret: secret
};
// there is other all functions, 2 of them i have already written on top
var signature = getMessageSignature(path, params, nonce);
var options = multipartFormData(params, config.url, config.key, nonce, signature);
request.post(options, function(error, response, body) {
...
}
}
我在请求正文中遇到此错误。错误:
消息:'给出了HMAC身份验证密钥和签名,但是它们 无效