1

我已经阅读了一些 node.js 文档(加密、https)并尝试在我的 coin.js 文件中使用coinbase.com API来获取我账户的比特币余额。但是仍然存在一些错误。当我使用正确的密钥和秘密值运行此代码时,我得到:[SyntaxError: Unexpected end of input] error parsing 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' }
4

1 回答 1

0

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

ACCESS_KEY 标头只是您的 API 密钥。

ACCESS_SIGNATURE 标头...

...您的请求中的 ACCESS_NONCE 标头...

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

尝试以这种方式设置选项:

var options = {
    method: 'GET',
    path:   '/api/v1/account/balance',
    hostname: 'coinbase.com',
    headers: {
        ACCESS_KEY: key,
        ACCESS_SIGNATURE: signature,
        ACCESS_NONCE: nonce,
    }
};
于 2014-05-16T13:59:45.593 回答