...
...
...
appanvil-karma- |
---|
...
designer |
---|
...
location | top |
---|
...
|
Table of Content Zone | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Getting StartedThe non-interactive login method for the Betfair Exchange API requires that you create and upload a self-signed certificate which will be used, alongside your username and password to authenticate your credentials and generate a session token. For the purposes of this guide, we have used OpenSSL to generate this client, details of which can be found at http://www.openssl.org/
Getting StartedThere are a couple of steps required before we can actually log in:
Creating a Self Signed CertificateThe Betfair Exchange API requires that a 1024-bit or 2048-bit RSA certificate be used. We are using OpenSSL in the below example but there are other ways of doing this, including using a UI-based interface such as XCA as covered in the Certificate Generation With XCA tutorial. Create a public/private RSA key pair using openssl
Update or Create the openssl configuration file (openssl.cnf) for OpenSSL to override some of the default settings:
Create a certificate signing request (CSR).
Self-sign the certificate request to create a certificate
Linking the Certificate to Your Betfair AccountThe previous steps should have created the following files:
Before you login using the certificate, it must be attached to your Betfair account, as follows:
Scroll down to the “Automated Betting Program Access” section if required and the certificate details should be shown. You should now be able to log in to your Betfair account using the Betfair API endpoint. Note on File FormatsSome systems require that client certificates are in a different format to the ones we’ve created. The two most common formats are (a) PEM format key and certificate in a single file and (b) PKCS#12 format file. .NET applications require a PKCS#12 a PKCS#12 format file. To create a PEM format file that contains both the private key and the certificate you can use the following command: Linux
Create the PKCS#12 format using crt and key
Details of a Login RequestA login request can now be made as follows:
Certificate Login Interface Details
URL Definition Code Block | Certificate Endpoint
This endpoint is also available under the following jurisdictions Please use the below if your country of residence is in one of the list jurisdictions.
Request headers
Request Parameters
ResponseThe response returned is a json string. If the response is successful then the loginStatus key will contain SUCCESS, for example:
Should a failure or exception be returned, the response will be structured as below and loginStatus will contain a failure reason:
The possible failure and exceptional return codes are:
Agent Client Master | AGENT_CLIENT_MASTER_SUSPENDED | Suspended Agent Client Master | DANISH_AUTHORIZATION_REQUIRED | Danish authorization required | SPAIN_MIGRATION_REQUIRED | Spain migration required | DENMARK_MIGRATION_REQUIRED | Denmark migration required | SPANISH_TERMS_ACCEPTANCE_REQUIRED | The latest Spanish terms and conditions version must be accepted. You must login to the website to accept the new conditions. | ITALIAN_CONTRACT_ACCEPTANCE_REQUIRED | The latest Italian contract version must be accepted. You must login to the website to accept the new conditions. | CERT_AUTH_REQUIRED | Certificate required or certificate present but could not authenticate with it. Please check that the correct file path is specified and ensure you are entering the correct password. Please note: You should ensure that your username and password values are encoded before being sent to the API; if your password contains special characters and isn't encoded, the login request will fail CHANGE_PASSWORD_REQUIRED | Change password required | PERSONAL_MESSAGE_REQUIRED | Personal message required for the user | INTERNATIONAL_TERMS_ACCEPTANCE_REQUIRED | The latest international terms and conditions must be accepted prior to logging in. | EMAIL_LOGIN_NOT_ALLOWED | This account has not opted in to log in with the email | MULTIPLE_USERS_WITH_SAME_CREDENTIAL | There is more than one account with the same credential ACCOUNT_PENDING_PASSWORD_CHANGE | The account must undergo password recovery to reactivate via https://identitysso.betfair.com/view/recoverpassword | TEMPORARY_BAN_TOO_MANY_REQUESTS | The limit for successful login requests per minute has been exceeded. New login attempts will be banned for 20 minutes | ITALIAN_PROFILING_ACCEPTANCE_REQUIRED | You must login to the website to accept the new conditions | AUTHORIZED_ONLY_FOR_DOMAIN_RO | You are attempting to login to the Betfair Romania domain with a non .ro account. | AUTHORIZED_ONLY_FOR_DOMAIN_SE | You are attempting to login to the Betfair Swedish domain with a non .se account. | SWEDEN_NATIONAL_IDENTIFIER_REQUIRED | You must provided your Swedish National identifier via Betfair.se before proceeding. | SWEDEN_BANK_ID_VERIFICATION_REQUIRED | You must provided your Swedish bank id via Betfair.se before proceeding. | ACTIONS_REQUIRED | You must login to https://www.betfair.com to provide the missing information. | INPUT_VALIDATION_ERROR | There is a problem with the data validity contained within the request. Please check that the request (including headers) is in the correct format,
Sample curl command to test the non-interactive loginUsing the below curl command, you should insert your own Betfair username and password. Please note: You must also specify the full file path that contains your client certificate and key.
Powershell Request
Postman request to test the Non-Interactive loginTo test the non-interactive login using Postman, you must first download Postman via https://www.postman.com/ The following instructions show you add to add the certificate and key file before making the login request: 1.Add certificate: Settings -> Certificates tab -> insert Host, upload .crt and .key file and click the Add button 2. Insert the endpoint URL (e.g. https://identitysso-cert.betfair.com/api/certlogin) and the request headers 3. Insert the request body as shown below: 4. Perform the call by clicking Send button. Sample Code for Non-Interactive Login
Sample C# code using PKCS#12 key storePlease see code sample via https://github.com/betfair/API-NG-sample-code/tree/master/loginCode/Non-interactive-cSharp Sample Java code using Apache http client library and PKCS#12 key store Code Block | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
language | java |
Code Block | ||
---|---|---|
| ||
package com.test.aping.client; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.ssl.StrictHostnameVerifier; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.security.KeyStore; import java.security.SecureRandom; import java.util.ArrayList; import java.util.List; public class HttpClientSSO { private static int port = 443; public static void main(String[] args) throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); try { SSLContext ctx = SSLContext.getInstance("TLS"); KeyManager[] keyManagers = getKeyManagers("pkcs12", new FileInputStream(new File("C:\\sslcerts\\client-2048.p12")), "password"); ctx.init(keyManagers, null, new SecureRandom()); SSLSocketFactory factory = new SSLSocketFactory(ctx, new StrictHostnameVerifier()); ClientConnectionManager manager = httpClient.getConnectionManager(); manager.getSchemeRegistry().register(new Scheme("https", port, factory)); HttpPost httpPost = new HttpPost("https://identitysso-cert.betfair.com/api/certlogin"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("username", "testuser")); nvps.add(new BasicNameValuePair("password", "testpassword")); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); httpPost.setHeader("X-Application","appkey"); System.out.println("executing request" + httpPost.getRequestLine()); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { String responseString = EntityUtils.toString(entity); //extract the session token from responsestring System.out.println("responseString" + responseString); } } finally { httpClient.getConnectionManager().shutdown(); } } private static KeyManager[] getKeyManagers(String keyStoreType, InputStream keyStoreFile, String keyStorePassword) throws Exception { KeyStore keyStore = KeyStore.getInstance(keyStoreType); keyStore.load(keyStoreFile, keyStorePassword.toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(keyStore, keyStorePassword.toCharArray()); return kmf.getKeyManagers(); } } |
Sample Python code
Code Block | ||
---|---|---|
| ||
#!/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('https://identitysso-cert.betfair.com/api/certlogin', 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." |
...