javascript - Coinbase.com API + Node.js 中的 secret HMAC 身份验证

标签 javascript node.js cryptography hmac coinbase-api

我已经阅读了一些node.js文档(加密货币,https)并尝试使用coinbase.com API在我的 coin.js 文件中获取我帐户的比特币余额。但仍然存在一些错误。当我使用正确的 key 和 secret 值运行此代码时,我得到: [SyntaxError: Unexpected end of input] 解析 json 时出错。

预先感谢您的回复。

var async   = require('async');
var http    = require('http');
var https   = require('https');
var crypto = require('crypto');

var key = 'some_key_from_coinbasecom';
var secret = 'some_secret_from_coinbasecom';  

var nonce = String(Date.now() * 1e6);
var url = 'https://coinbase.com/api/v1/account/balance';
var message = nonce + url + ''; // if body is not empty then put here body
var signature = crypto.createHmac('sha256', secret).update(message).digest('hex');

var options = {
    method: 'GET',
    path:   '/api/v1/account/balance',
    ACCESS_KEY: key,
    ACCESS_SIGNATURE: signature,
    ACCESS_NONCE: nonce,
    hostname: 'coinbase.com'
};

  https.get(options, function(res) {
    var body = '';
    res.on('data', function(chunk) {body += chunk;});
    res.on('end', function() {

      try {
        var balance_json = JSON.parse(body);
        if (balance_json.error) {
        console.log(balance_json.error);
          return;
        } else {
    // Here I have expected to get my balance json data
      console.log(balance_json);
        }
      } catch (error) {
    console.log(error);
    console.log("error parsing json");
      }
    });
    res.on('error', function(e) {
       console.log(e);
      console.log("error syncing balance");
    });
  });

我尝试了另一种实现:

https.get(options, function(res) {

    console.log("statusCode: ", res.statusCode);
  console.log("headers: ", res.headers);

  res.on('data', function(d) {
    process.stdout.write(d);
  });

}).on('error', function(e) {
  console.error(e);
});

此代码的结果(我通过 XXXXXXXXXX 隐藏了一些字符串):

statusCode:  401
headers:  { server: 'cloudflare-nginx',
  date: 'Wed, 14 May 2014 17:21:09 GMT',
  'content-type': 'text/html; charset=utf-8',
  'transfer-encoding': 'chunked',
  connection: 'keep-alive',
  'set-cookie': 
   [ '__cfduid=deacXXXXXXXXXXXXXXXXXXXXXXXXXXXX1400088069337; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.coinbase.com; HttpOnly',
     'request_method=GET; path=/; secure' ],
  'cache-control': 'no-cache, no-store, max-age=0, must-revalidate',
  expires: '-1',
  pragma: 'no-cache',
  status: '401 Unauthorized',
  'strict-transport-security': 'max-age=31536000',
  vary: 'Accept-Encoding',
  'www-authenticate': 'Bearer realm="Doorkeeper", error="invalid_token", error_description="The access token is invalid"',
  'x-content-type-options': 'nosniff',
  'x-frame-options': 'SAMEORIGIN',
  'x-rack-cache': 'miss',
  'x-request-id': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  'x-runtime': '0.031708',
  'x-ua-compatible': 'IE=Edge,chrome=1',
  'cf-ray': 'XXXXXXXXXXXXXXXXXXXXXXXXXX-LHR' }

最佳答案

来自https://coinbase.com/docs/api/authentication :

The ACCESS_KEY header is simply your API key.

The ACCESS_SIGNATURE header ...

... ACCESS_NONCE header in your requests ...

http://nodejs.org/api/https.html文档中你可以发现 headers 被放入特定的 options.headers 对象中

尝试这样设置选项:

var options = {
    method: 'GET',
    path:   '/api/v1/account/balance',
    hostname: 'coinbase.com',
    headers: {
        ACCESS_KEY: key,
        ACCESS_SIGNATURE: signature,
        ACCESS_NONCE: nonce,
    }
};

关于javascript - Coinbase.com API + Node.js 中的 secret HMAC 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23652750/

相关文章:

Javascript 从出生日期计算年龄

javascript - 获取数组内对象的名称

javascript - jQuery 添加多个具有相同名称的 css 属性不起作用

javascript - JSON 键作为 JavaScript 中的 JSON 值

javascript - 是否可以选择溢出特定 div 的所有元素并给它一些 CSS 并放置在另一个 div 中

node.js - Mongoose 更新子文档数组

javascript - dust.js 随机用户上传的模板是否可以安全地在服务器端执行?

java - AES javascript加密和Java解密

Java Cipher 表示 key 长度无效

java - EncryptionException : javax. crypto.IllegalBlockSizeException:使用填充密码解密时,输入长度必须是 8 的倍数