DISCLAIMER

DISCLAIMER: The content of the blog made for educational purposes only. Author of this blog does not take responsibility for any loss or damage which may be caused by using of the software code provided on this blog.

Saturday, 9 May 2015

Login

Today I try to make a login/logout procedure to my account using Application key and certificate generate in previous post.
I'm going to use standard Powershell ISE on Windows 7.
I have upgraded my Powershell to version 4.0 as the standard one was 2.0.
I have to enable script execution by executing:
Set-ExecutionPolicy Unrestricted

Next I'm going to write a bit of code.
First I define global variables, such as URLs, keys,  HTTP headers
1:  #main URLs  
2:  $loginURL="https://identitysso.betfair.com/api/certlogin"  
3:  $logoutURL = "https://identitysso.betfair.com/api/logout"  
4:  #path to cetificate file  
5:  $certFilePath=".\client-2048.crt"  
6:    
7:  #load certificate  
8:  $cert = Get-PfxCertificate $certFilePath  
9:  #get thumbPrint  
10:  $thumbPrint = $cert.Thumbprint  
11:    
12:  #application Key  
13:  $xApplicationKey="my_app_key"  
14:    
15:  #credentials  
16:  $username="username"  
17:  $password="password"  
18:    
19:  #global variables for HTTP requests  
20:  $contentType=“application/x-www-form-urlencoded”  
21:  $header = @{  
22:  "X-Application"=$xApplicationKey  
23:  }  

Second I define a login function:
1:  Function login {  
2:    
3:  #set body  
4:  $body = @{  
5:  "username"=$username  
6:  "password"=$password  
7:  }  
8:    
9:  #call Betfair API NG  
10:  $r=Invoke-RestMethod -uri $loginURL -Body $body -Certificate $cert -CertificateThumbprint $thumbPrint -ContentType $contentType -Header $header -Method "POST" -SessionVariable bsv  
11:    
12:  #chek if login was a success  
13:  if ( $r.loginStatus -ne "SUCCESS" ) {  
14:    Write-Host "ERROR: Loginfailed $($r)"  
15:    exit(1)  
16:  } else {  
17:   Write-Host "Login status: $($r.loginStatus)"  
18:  }  
19:    
20:  #return session token  
21:  return $r.sessionToken  
22:  }  

When I execute this code in Powershell ISE, I got Login status: success on the screen.

Prep work

API NG is a new API Betfair developed. It uses REST/JSON technology. Before that they had an API based on SOAP which is still in use and as far as I know there is no set date to discontinue it.
Perhaps they are waiting so all the major developers to switch their apps to API NG.
In order to start one needs three things:
1. a Betfair account
2. an Application key
3. session Token

I already had a Betfair account for a while, so all I need is to read an API NG doc and try to do the other two.
API NG docs

An application key is a string which probably generated by some algorithm. In fact there are two of those. One is for normal API and another for a test/delayed API, so one can use it to test the code.
Application key, step by step

The third bit would be to create a session token which requires to create a certificate and register it with Betfair.
Certificate, step by step

First the certificate must be generated. To generate a certificate one need to install openssl. This tool can be downloaded, its an open source SSL libs and tools.
Those are my steps I took to generate a certificate:

"c:\Program Files (x86)\OpenSSL\bin\openssl.exe" genrsa -out client-2048.key 2048

 "c:\Program Files (x86)\OpenSSL\bin\openssl.exe" req -new -config openssl.cnf -key client-2048.key -out client-2048.csr

"c:\Program Files (x86)\OpenSSL\bin\openssl.exe" x509 -req -days 365 -in client-2048.csr -signkey client-2048.key -out client-2048.crt -extfile "c:\Program Files (x86)\OpenSSL\openssl.cnf" -extensions ssl_client


"c:\Program Files (x86)\OpenSSL\bin\openssl.exe" pkcs12 -export -in client-2048.crt -inkey client-2048.key -out client-2048.p12

Once certificate was generated, it need to be added to Windows OS certificate manager.
In order to do that one need to start certmgr.msc, navigate to the correct branch E.g. Personal/Certificates and choose import from the context menu.
Always select PFX/P12 file, not the CRT file!










After this just follow the steps to link the certificate to your Betfair account:
Link certificate to Betfair account

Once this is done you are ready to start coding.

Introduction

I have stared this blog to document my learning curve in Betfair API NG. All the materials and documentation I pick is from open sources in Internet. My goal is to learn how to create an unattended process which will trade on Betfair exchange. I do not have a goal to become a millionaire overnight and retire in two years. My chosen language would be Windows Powershell 4.0 as I own a Windows laptop, Powershell 4.0 is mature enough to do a lot of things out of the box and scripting is much easier in quick modification, code adjustment while debugging than developing full scale, compiled application. I'll publish all the code here, as I progress. If you try to use the code and experience any loss, I do not except any responsibility for the loss . You should completely understand that this application makes financial transactions and the outcome of those can be a loss.