In this session we are now ready to start talking to Betfair through the API
Step 1 Double click on your desktop Python IDLE icon to open up the Python development tool
Step 2 Click the file tab followed by new file
Step 3 In the new file window that has just appeared paste in the following code. (NOTE indentation is important in Python, it delimits the scope of an IF or a FOR loop so make sure you paste as is with indentation in place)
#!/usr/bin/env python
#API-NG library
import requests
import urllib
import urllib.request
import urllib.error
import json
import datetime
import sys
import time
############################################################
def loginAPING(userName,passWord):
### returns session token or -1 if fails ###
appKey = “1” # seems to be needed although not a valid appkey
payload = 'username='+userName+'&password='+passWord
headers = {'X-Application': appKey,'Content-Type': 'application/x-www-form-urlencoded'}
resp = requests.post('https://identitysso.betfair.com/api/certlogin ', data=payload, cert=('client-2048.crt', 'client-2048.key'), headers=headers)
resp_json = resp.json()
if resp_json['loginStatus'] == 'SUCCESS':
return resp_json['sessionToken']
else:
return -1
#############################################################
Step 4 Save the file with the file name myAPILib.py
Step 5 Open another new file and paste in the following text
#!/usr/bin/env python
import myAPILib
sessionToken = myAPILib.loginAPING("yourusername","yourpassword")
print (sessionToken)
In the above substitute your Betfair account name and password
Save the file as testlib.py
Step 6 In the testlib window click the run tab and select run module
Step 7 Output will appear in the shell window and it should print out your session taken for the login just performed by the program. If it failed it will have printed out the number -1
Well done you now have a program that is using a python library to execute commands to the Betfair API. You have successfully logged in and we can in the next session look at how to get hold of race ID’s for the day along with race off times.
I had a little trouble with this session for two reasons:
1. It turns out that Python is very specific about indentation (I know nothing about Python!) so when I copied the code into IDLE it just generated an error. I suspect that the formatting of your post has taken all of the indentation out of the code, so you might need to find a WordPress command that allows code to be displayed as normal (something akin to the PRE tag in HTML)
2. The URL used should stop after certlogin and be terminated by a single quote, and not have the extra characters that appear to have been inserted (again I suspect that this was done automatically when the code was copied to WordPress).
Looking forward to the next set of sessions.
Thanks Alistair, very good points, I will look into this later this morning and see if I can get them sorted out
Sorted those niggles out, you are right WordPress is not very helpful with certain characters. The code can be copied and pasted now and works fine. Thanks for the heads up
do you mind showing exactly which parts of step 3 need to be copied and pasted i keep getting an indentation error or a unexpected EOF error
thanks
Hi, yes indentation is very important in Python. With other languages its just encouraged but in Python it actually defines the cope of an IF statement or a FOR loop. WordPress is not being very helpful on this issue as its not exactly a WYSIWYG editor. I will add a comment about indentation, thanks for the feedback
Slight change to the code made on this session as at 06:35am on 24/4/2014
Hi there
I have followed all the steps – got my certificate , uploaded to Betfair but failed on this step
maybe you can give me some references as to where you started with this whole process , did you write any of the library , reading points , websites etc
please contact me
I have xxxxxxxx out my username/email and password for obvious reasons but below is the error log
Traceback (most recent call last):
File “C:\Users\Clive\AppData\Local\Programs\Python\Python36\lib\site-packages\requests-2.17.3-py3.6.egg\requests\utils.py”, line 868, in check_header_validity
if not pat.match(value):
TypeError: expected string or bytes-like object
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:/Users/Clive/Documents/Betfair/Python/testlib.py”, line 5, in
sessionToken = myAPILib.loginAPING(“XXXXXXXXXXX@gmail.com”,”XXXXXXXXXXXX”)
File “C:/Users/Clive/Documents/Betfair/Python\myAPILib.py”, line 25, in loginAPING
resp = requests.post(‘https://identitysso.betfair.com/api/certlogin ‘, data=payload, cert=(‘client-2048.crt’, ‘client-2048.key’), headers=headers)
File “C:\Users\Clive\AppData\Local\Programs\Python\Python36\lib\site-packages\requests-2.17.3-py3.6.egg\requests\api.py”, line 112, in post
return request(‘post’, url, data=data, json=json, **kwargs)
File “C:\Users\Clive\AppData\Local\Programs\Python\Python36\lib\site-packages\requests-2.17.3-py3.6.egg\requests\api.py”, line 58, in request
return session.request(method=method, url=url, **kwargs)
File “C:\Users\Clive\AppData\Local\Programs\Python\Python36\lib\site-packages\requests-2.17.3-py3.6.egg\requests\sessions.py”, line 499, in request
prep = self.prepare_request(req)
File “C:\Users\Clive\AppData\Local\Programs\Python\Python36\lib\site-packages\requests-2.17.3-py3.6.egg\requests\sessions.py”, line 431, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File “C:\Users\Clive\AppData\Local\Programs\Python\Python36\lib\site-packages\requests-2.17.3-py3.6.egg\requests\models.py”, line 304, in prepare
self.prepare_headers(headers)
File “C:\Users\Clive\AppData\Local\Programs\Python\Python36\lib\site-packages\requests-2.17.3-py3.6.egg\requests\models.py”, line 438, in prepare_headers
check_header_validity(header)
File “C:\Users\Clive\AppData\Local\Programs\Python\Python36\lib\site-packages\requests-2.17.3-py3.6.egg\requests\utils.py”, line 872, in check_header_validity
“not %s” % (value, type(value)))
requests.exceptions.InvalidHeader: Header value 1 must be of type str or bytes, not
>>>
There seems to have been a change in the API. The line that previously said appKey = 1 worked fine until recently but now it appears that the appkey must be of type string and hence the change shown above to appKey= “1”
Smartersig thanks it works now but sorry to be a pain . In the interests of educating myself how did that log lead you to that solution
regards
Clive
The final line says header value 1 must be of type string or bits
Thank you for the guide, it is good.
When I run my code, I always receive a “-1” output, I understand this means it has failed, but how/why does it fail in this way?
Your feedback would be much appreciated.