1

node.js ドキュメント (crypto、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 に答える