Node.js bot login example

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AlgoTrader
    Junior Member
    • Mar 2012
    • 243

    #1

    Node.js bot login example

    I finally implemented bot login in node.js. It proved to be much simpler then C# and very comparable to Python. I use C# at work, but I certainly prefer JavaScript/Python for Betfair hobby development. So the code is:

    Code:
    var https = require('https');
    var fs = require('fs');
    
    var data = 'username=nope&password=nope';
    
    var options = {
      hostname: 'identitysso-api.betfair.com',
      port: 443,
      path: '/api/certlogin',
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': data.length,
        'X-Application': 'test'
      },
      key: fs.readFileSync('client-2048.key'),
      cert: fs.readFileSync('client-2048.crt')
    };
    options.agent = new https.Agent(options);
    
    var req = https.request(options, function(res) {
      console.log("statusCode:", res.statusCode);
    
      var responseData = "";
      res.on('data', function(d) {
         responseData += d;
      });
      res.on('end', function(d) {
         var response = JSON.parse(responseData);
         console.log("sessionToken:", response.sessionToken.replace(/\d/g, ''));
      });
      res.on('error', function(e) {
        console.error(e);
      });
    });
    
    req.end(data);
    The result is:
    Code:
    C:\Projects\BetfairKey>node botlogin.js
    statusCode: 200
    sessionToken: <sessionToken>
    The code above removes all digits from the token to save my account.

    The code is not optimized, I can do it shorter by 3-5 lines, but it is still very copmparable to Python. All the tests were in Windows, but it will work in Linux and MacOSX also.

    Many steps of like making config.ssl and making pcs12 are optional for JavaScript. We do not need any certificate storage, any PCS12 or any PEM combining keys and certificates. Just .key and .crt files in current directory. We need a PEM to upload it to Betfair site though
    Last edited by Mr Stokes; 22-08-2013, 10:55 AM. Reason: session token removed
    Betfair Bots Made Easy
  • AlgoTrader
    Junior Member
    • Mar 2012
    • 243

    #2
    Minor cleanup

    Code:
    var https = require('https');
    var fs = require('fs');
    var url = require('url');
    
    var uri = "https://identitysso-api.betfair.com:443/api/certlogin";
    var data = 'username=nope&password=nope';
    
    var options = url.parse(uri);
    options.method = 'POST';
    options.headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'X-Application': 'test'
      };
    options.key = fs.readFileSync('client-2048.key');
    options.cert = fs.readFileSync('client-2048.crt');
    options.agent = new https.Agent(options);
    
    var req = https.request(options, function(res) {
      console.log("statusCode:", res.statusCode);
      var responseData = "";
      res.on('data', function(d) {
         responseData += d;
      });
      res.on('end', function() {
         var response = JSON.parse(responseData);
         console.log("sessionToken:", response.sessionToken.replace(/\d/g, ''));
      });
      res.on('error', function(e) {
        console.error(e);
      });
    });
    
    req.end(data);
    Just a native node.js, no any third party libs. I removed the scroll, now all the stuff is visible
    Betfair Bots Made Easy

    Comment

    • toby1kenobi.
      Junior Member
      • Dec 2013
      • 1

      #3
      This is great, thanks for sharing!

      Toby

      Comment

      • merecat_
        Junior Member
        • May 2009
        • 35

        #4
        Yes thanks for sharing this.

        I run some .js from Java (far quicker to make tweaks within the .js files than Java)

        I must take the time to look at node.js

        What tools do you use to debug your .js?

        Comment

        • ittechnician
          Junior Member
          • Nov 2014
          • 1

          #5
          Getting { loginStatus: 'CERT_AUTH_REQUIRED' } response

          Hi guys,

          New to this forum. Seems like a good community.

          I'm getting { loginStatus: 'CERT_AUTH_REQUIRED' } as a response when I try and use @AlgoTrader's code exactly. Any ideas why? Curl has it's own problems on a Mac.

          And @merecat_ I use the following debugger https://www.npmjs.org/package/node-inspector

          Thanks

          Comment

          Working...
          X