【发布时间】:2015-08-18 19:16:54
【问题描述】:
我正在尝试使用带有 LocalBitcoins API 的 HMAC 进行身份验证。
这是用 Python 编写的认证:
message = str(nonce) + hmac_auth_key + relative_path + get_or_post_params_urlencoded
signature = hmac.new(hmac_auth_secret, msg=message, digestmod=hashlib.sha256).hexdigest().upper()
以及创建HMAC消息的参数:
Nonce. A 63 bit positive integer, for example unix timestamp as milliseconds.
HMAC authentication key. This is the first one of a key/secret pair.
Relative path, for example /api/wallet/.
GET or POST parameters in their URL encoded format, for example foo=bar&baz=quux.
这是我构建 HMAC 的方式:
var milliseconds = (new Date).getTime();
var key = config.key;
var secret = config.secret;
var nonce = milliseconds.toString()
var message = nonce + key + 'api/myself';
var hmac_digest = crypto.createHmac("sha256", secret).update(message).digest('hex').toUpperCase();
签名通过 3 个 HTTP 标头发送。调用 api/myself 方法的选项如下所示(使用请求):
{ url: 'https://localbitcoins.com/api/myself',
method: 'GET',
headers:
{ 'Apiauth-Key': 'my api key',
'Apiauth-Nonce': 1439925212276,
'Apiauth-Signature': 'the created signature' },
timeout: 5000 }
请求:
var req = request.get(options, function(error, response, body) {
console.log(body);
});
但每次我收到以下错误消息:
{ error:
{ message: 'HMAC authentication key and signature was given, but they are invalid.',
error_code: 41 } }
我在测试中尝试了很多不同的组合,但没有任何效果。我错过了什么?
【问题讨论】:
-
我不确定这是否可行,您是否尝试过使用 OAuth 进行身份验证?
-
我没看过。在这一点上仍然超级喜欢 HMAC。