Swift
Swift
ABN AMRO OAuth2 Client Credentials Authentication
See more ABN AMRO Examples
Demonstrates how to obtain an access token for an ABN AMRO online API using OAuth2 with the Client Credentials flow.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example sends the following CURL request:
// curl -X POST -k https://auth-sandbox.connect.abnamro.com:8443/as/token.oauth2 \
// -v \
// --cert TPPCertificate.crt \
// --key TPPprivateKey.key \
// -H 'Cache-Control: no-cache' \
// -H 'Content-Type: application/x-www-form-urlencoded' \
// -d 'grant_type=client_credentials&client_id=TPP_test&scope=psd2:payment:sepa:write psd2:payment:sepa:read'
let cert = CkoCert()!
success = cert.load(fromFile: "qa_data/certs/TPPCertificate.cer")
if success == false {
print("\(cert.lastErrorText!)")
return
}
let bdKey = CkoBinData()!
success = bdKey.loadFile(path: "qa_data/certs/TPPprivateKey.key")
let privKey = CkoPrivateKey()!
success = privKey.loadAnyFormat(privKeyData: bdKey, password: "passwordIfNeeded")
if success == false {
print("\(privKey.lastErrorText!)")
return
}
success = cert.setPrivateKey(privKey: privKey)
if success == false {
print("\(cert.lastErrorText!)")
return
}
let http = CkoHttp()!
success = http.setSslClientCert(cert: cert)
if success == false {
print("\(http.lastErrorText!)")
return
}
let req = CkoHttpRequest()!
req.addParam(name: "grant_type", value: "client_credentials")
req.addParam(name: "client_id", value: "TPP_test")
req.addParam(name: "scope", value: "psd2:payment:sepa:write psd2:payment:sepa:read")
req.httpVerb = "POST"
req.contentType = "application/x-www-form-urlencoded"
let resp = CkoHttpResponse()!
success = http.httpReq(url: "https://auth-sandbox.connect.abnamro.com:8443/as/token.oauth2", request: req, response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
if resp.statusCode.intValue != 200 {
print("\(resp.bodyStr!)")
return
}
// Get the JSON result:
// {"access_token":"TIhycwl8rfrZPkXGw15mwldASAAK","token_type":"Bearer","expires_in":7200}
let json = CkoJsonObject()!
json.load(json: resp.bodyStr)
print("access_token: \(json.string(of: "access_token")!)")
print("token_type: \(json.string(of: "token_type")!)")
print("expires_in: \(json.string(of: "expires_in")!)")
}