Bitcoin Forum
November 07, 2024, 09:06:00 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How to auth. with MtGox API v2 (node.js) ?  (Read 1235 times)
tagada (OP)
Member
**
Offline Offline

Activity: 74
Merit: 10


View Profile
April 19, 2013, 03:34:13 PM
 #1

Hello,

I can't authenticate with MtGox API (v2)
I use the following function :

Code:
var querystring = require('querystring'),
        https = require('https'),
        crypto = require('crypto');
 
function MtGoxClient() {
     this.key="my_api_key_here";
     this.secret ="mysecret_key_here";
}
 
MtGoxClient.prototype.query = function(path, args, callback) {
        var client = this;
        // if no args or invalid args provided, just reset the arg object
        if (typeof args != "object") args = {};
 
        // generate a nonce
        args['nonce'] = (new Date()).getTime() * 1000;
        // compute the post data
        var post = querystring.stringify(args);
        // compute the sha512 signature of the post data
         hash_data = path  + post;
        var hmac = crypto.createHmac('sha512',new Buffer(client.secret, 'base64'));
        hmac.update(hash_data);
        // this is our query
        var options = {
                host: 'data.mtgox.com',
                port: 443,
                path: '/api/' + path,
                method: 'POST',
                agent: false,
                headers: {
                        'Rest-Key': client.key,
                        'Rest-Sign': hmac.digest('base64'),
                        'User-Agent': 'Mozilla/4.0 (compatible; MtGox node.js client)',
                        'Content-type': 'application/x-www-form-urlencoded',
                        'Content-Length': post.length
                }
        };
        // run the query, buffer the data and call the callback
        var req = https.request(options, function(res) {           
                res.setEncoding('utf8');
                var buffer = '';
                res.on('data', function(data) { buffer += data; });
                res.on('end', function() { if (typeof callback == "function") { callback(JSON.parse(buffer)); } });
        });
        // basic error management
        req.on('error', function(e) {
                console.log('warning: problem with request: ' + e.message);
        });
        // post the data
        req.write(post);
        req.end();
};
 
var client = new MtGoxClient();
client.query('2/BTCUSD/money/info', {}, function(json) {       
        console.log(json);
});

and always get :
Code:
{ result: 'error',
  error: 'Identification required to access private API',
  token: 'login_error_invalid_rest_sign' }

What am I doing wrong?
after hours of trying, any help will reaaaaaaaally be appreciated Smiley
tagada (OP)
Member
**
Offline Offline

Activity: 74
Merit: 10


View Profile
May 26, 2013, 05:57:18 PM
 #2

I'm posting this PM from user ameen3 who kindly messaged me this solution, in the hope that it might help anyone else stuck with the same pb:

Quote from: ameen3
I'm still a newbie, so I can't post on the forums yet, but I also had trouble connecting to the new API.  

It comes down to not only including the querystring of the post parameters to the message hash, but the path as well.  

Code:
var post = querystring.stringify(args);
// append the path to the post data
var message = path + "\0" + post;

You can see the full code here: https://github.com/ameen3/node-mtgox-apiv2

If you can, post this message in a reply to your thread because alas, I'm a newbie.  

Hope this helps.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!