Hello Betfair programmers, could you please share your code with newbie?
or correct this code
import Foundation
import UIKit
import Security
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// let sess = SessionToken()
login()
// Do any additional setup after loading the view.
}
}
func login(){
let appId = ""
let username = ""
let password = ""
let encodedUsername = username.addingPercentEncoding(withAllowedCharacte rs: .urlQueryAllowed)!
let encodedPassword = password.addingPercentEncoding(withAllowedCharacte rs: .urlQueryAllowed)!
let urlString = "https://identitysso-cert.betfair.com/api/certlogin"
let url = URL(string: urlString)!
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
//print Bundle.main.path
let clientCertPath = Bundle.main.path(forResource: "TestApp1", ofType: "crt")!
let clientCertData = try! Data(contentsOf: URL(fileURLWithPath: clientCertPath))
let clientKeyPath = Bundle.main.path(forResource: "TestApp1", ofType: "key")!
let clientKeyData = try! Data(contentsOf: URL(fileURLWithPath: clientKeyPath))
let certAndKey = clientCertData + clientKeyData
let certAndKeyBase64 = certAndKey.base64EncodedString()
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue(appId, forHTTPHeaderField: "X-Application")
request.httpBody = "username=\(encodedUsername)&password=\(encode dPas sword)".data(using: .utf8)
let authValue = "Basic \(certAndKeyBase64)"
request.setValue(authValue, forHTTPHeaderField: "Authorization")
let task = session.dataTask(with: request) { data, response, error in
guard error == nil else {
print("Error: \(error!)")
return
}
guard let data = data, let response = response as? HTTPURLResponse else {
print("No data or response received.")
return
}
print("Response status code: \(response.statusCode)")
print("Response body: \(String(data: data, encoding: .utf8) ?? "")")
}
task.resume()
}
it has returned CERT_AUTH_REQUIRED
or correct this code
import Foundation
import UIKit
import Security
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// let sess = SessionToken()
login()
// Do any additional setup after loading the view.
}
}
func login(){
let appId = ""
let username = ""
let password = ""
let encodedUsername = username.addingPercentEncoding(withAllowedCharacte rs: .urlQueryAllowed)!
let encodedPassword = password.addingPercentEncoding(withAllowedCharacte rs: .urlQueryAllowed)!
let urlString = "https://identitysso-cert.betfair.com/api/certlogin"
let url = URL(string: urlString)!
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
//print Bundle.main.path
let clientCertPath = Bundle.main.path(forResource: "TestApp1", ofType: "crt")!
let clientCertData = try! Data(contentsOf: URL(fileURLWithPath: clientCertPath))
let clientKeyPath = Bundle.main.path(forResource: "TestApp1", ofType: "key")!
let clientKeyData = try! Data(contentsOf: URL(fileURLWithPath: clientKeyPath))
let certAndKey = clientCertData + clientKeyData
let certAndKeyBase64 = certAndKey.base64EncodedString()
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue(appId, forHTTPHeaderField: "X-Application")
request.httpBody = "username=\(encodedUsername)&password=\(encode dPas sword)".data(using: .utf8)
let authValue = "Basic \(certAndKeyBase64)"
request.setValue(authValue, forHTTPHeaderField: "Authorization")
let task = session.dataTask(with: request) { data, response, error in
guard error == nil else {
print("Error: \(error!)")
return
}
guard let data = data, let response = response as? HTTPURLResponse else {
print("No data or response received.")
return
}
print("Response status code: \(response.statusCode)")
print("Response body: \(String(data: data, encoding: .utf8) ?? "")")
}
task.resume()
}
it has returned CERT_AUTH_REQUIRED


Comment