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:
The result is:
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
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);
Code:
C:\Projects\BetfairKey>node botlogin.js statusCode: 200 sessionToken: <sessionToken>
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


Comment