Transition from manual session tokens to SSL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Philby
    Junior Member
    • Apr 2023
    • 10

    #1

    Transition from manual session tokens to SSL

    I access the betfair api using python code in a jupyter notebook. Up to now I manually type the session token into the 'header' each time, but I want to transition away from the need to do that - and instead use SSL.

    I followed the steps at the URL below, to set up SSL

    https://betfair-developer-docs.atlas...iveLoginSample

    and successfully was able to run the code below

    Code:
    #!/usr/bin/env python
     
    import requests
     
    #openssl x509 -x509toreq -in certificate.crt -out CSR.csr -signkey privateKey.key
     
     
    payload = 'username=myusername&password=password'
    headers = {'X-Application': 'SomeKey', 'Content-Type': 'application/x-www-form-urlencoded'}
     
    resp = requests.post('[URL]https://identitysso-cert.betfair.com/api/certlogin'[/URL], data=payload, cert=('client-2048.crt', 'client-2048.key'), headers=headers)
     
    if resp.status_code == 200:
      resp_json = resp.json()
      print resp_json['loginStatus']
      print resp_json['sessionToken']
    else:
      print "Request failed."​
    Which returned 'SUCCESS' and my session token for the 2 lines below

    Code:
    print(resp_json['loginStatus'])
    print(resp_json['sessionToken'])
    I now want to adjust all of my python code in my various jupyter notebooks, so that they login using SSL and retire the use of manually copied & pasted session tokens.

    I tried adjusting the code below in various ways but always ran into errors

    Original code

    Where APP_KEY_HERE and SESSION_TOKEN_HERE are manually typed in my local version.

    Code:
    ## Fetch competition IDs
    
    header = { 'X-Application' : 'APP_KEY_HERE', 'X-Authentication' : 'SESSION_TOKEN_HERE=' ,'content-type' : 'application/json' }
    endpoint = "https://api.betfair.com/exchange/betting/rest/v1.0/"
    
    json_req_lC='{"filter":{ "eventTypeIds" : [1] }}, "id": 1}'
    url_lC = endpoint + "listCompetitions/"
    
    response_listCompetitions = requests.post(url_lC, data=json_req_lC, headers=header)
    
    listCompetitions_data = response_listCompetitions.json()
    
    competition_ids = []
    competition_names = []
    competition_regions = []
    
    for item in listCompetitions_data:
    competition_ids.append(item['competition']['id'])
    competition_names.append(item['competition']['name'])
    competition_regions.append(item['competitionRegion'])
    
    df_competitions = pd.DataFrame({'Comp ID': competition_ids, 'Comp Name': competition_names, 'Comp Region': competition_regions})
    df_competitions = df_competitions .sort_values('Comp Region', ascending=True)
    
    df_competitions.head()
    Q: How do I adjust this code to make use of the SSL to call ​"https://api.betfair.com/exchange/betting/rest/v1.0/"?​
    Last edited by Philby; 01-10-2024, 11:41 AM.
Working...
X