Pergunta

I have read some node.js docs (crypto, https) and try to use coinbase.com API in my coin.js file to get bitcoin balance of my account. But there are still some errors. When I run this code with correct key and secret values I get: [SyntaxError: Unexpected end of input] error parsing json.

Thank you in advance for your responses.

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");
    });
  });

I have tried another implementation:

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);
});

There are results of this code (I have hidden some strings by 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' }
Foi útil?

Solução

From 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 ...

In http://nodejs.org/api/https.html document you can find that headers are put into specific options.headers objects

Try to set options this way:

var options = {
    method: 'GET',
    path:   '/api/v1/account/balance',
    hostname: 'coinbase.com',
    headers: {
        ACCESS_KEY: key,
        ACCESS_SIGNATURE: signature,
        ACCESS_NONCE: nonce,
    }
};
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top