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
Second I define a login function:
When I execute this code in Powershell ISE, I got Login status: success on the screen.
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.
No comments:
Post a Comment