PureBasic
PureBasic
Get Access Token using a Pre-Created JSON Web Token
See more ABN AMRO Examples
Demonstrates how to get an access token using a pre-created JSON Web Token (JWT).Chilkat PureBasic Downloads
IncludeFile "CkJsonObject.pb"
IncludeFile "CkJwt.pb"
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpRequest.pb"
IncludeFile "CkHttpResponse.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
; We're going to duplicate this CURL statement:
; curl -X POST https://api-sandbox.abnamro.com/v1/oauth/token \
; -H "Content-Type: application/x-www-form-urlencoded" \
; -H "API-Key: xxxxxx" \
; -d 'client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&grant_type=client_credentials&client_assertion=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ4eHh4eHgiLCJleHAiOiIxNDk5OTQ3NjY4IiwiaXNzIjoibWUiLCJhdWQiOiJodHRwczovL2F1dGgtc2FuZGJveC5hYm5hbXJvLmNvbS9vYXV0aC90b2tlbiJ9.jGwHKG_YjgKpR8NPpaLu6nJ97obeP2vcxg6fOWBKdJ0&scope=tikkie'
; Load our pre-creaed private key PEM file.
; Note: Please share your public key along with your app name and developer email id at api.support@nl.abnamro.com.
; Token generation will not work unless public key is associated with your app.
privkey.i = CkPrivateKey::ckCreate()
If privkey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPrivateKey::ckLoadPemFile(privkey,"qa_data/pem/abnAmroPrivateKey.pem")
If success = 0
Debug CkPrivateKey::ckLastErrorText(privkey)
CkPrivateKey::ckDispose(privkey)
ProcedureReturn
EndIf
; Create the JWT.
jwt.i = CkJwt::ckCreate()
If jwt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Create the header:
; {
; "typ": "JWT",
; "alg": "RS256"
; }
jsonHeader.i = CkJsonObject::ckCreate()
If jsonHeader.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(jsonHeader,"typ","JWT")
CkJsonObject::ckUpdateString(jsonHeader,"alg","RS256")
; Create the payload:
; {
; "nbf": 1499947668,
; "exp": 1499948668,
; "iss": "me",
; "sub": "anApiKey",
; "aud": "https://auth-sandbox.abnamro.com/oauth/token"
; }
jsonPayload.i = CkJsonObject::ckCreate()
If jsonPayload.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
curDateTime.i = CkJwt::ckGenNumericDate(jwt,0)
; Set the "not process before" timestamp to now.
success = CkJsonObject::ckAddIntAt(jsonPayload,-1,"nbf",curDateTime)
; Set the timestamp defining an expiration time (end time) for the token
; to be now + 1 hour (3600 seconds)
success = CkJsonObject::ckAddIntAt(jsonPayload,-1,"exp",curDateTime + 3600)
CkJsonObject::ckUpdateString(jsonPayload,"iss","me")
CkJsonObject::ckUpdateString(jsonPayload,"sub","anApiKey")
CkJsonObject::ckUpdateString(jsonPayload,"aud","https://auth-sandbox.abnamro.com/oauth/token")
; Produce the smallest possible JWT:
CkJwt::setCkAutoCompact(jwt, 1)
jwtStr.s = CkJwt::ckCreateJwtPk(jwt,CkJsonObject::ckEmit(jsonHeader),CkJsonObject::ckEmit(jsonPayload),privkey)
If CkJwt::ckLastMethodSuccess(jwt) = 0
Debug CkJwt::ckLastErrorText(jwt)
CkPrivateKey::ckDispose(privkey)
CkJwt::ckDispose(jwt)
CkJsonObject::ckDispose(jsonHeader)
CkJsonObject::ckDispose(jsonPayload)
ProcedureReturn
EndIf
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
req.i = CkHttpRequest::ckCreate()
If req.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpRequest::ckAddParam(req,"client_assertion_type","urn:ietf:params:oauth:client-assertion-type:jwt-bearer")
CkHttpRequest::ckAddParam(req,"grant_type","client_credentials")
CkHttpRequest::ckAddParam(req,"client_assertion",jwtStr)
CkHttpRequest::ckAddParam(req,"scope","tikkie")
CkHttpRequest::setCkHttpVerb(req, "POST")
CkHttpRequest::setCkContentType(req, "application/x-www-form-urlencoded")
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpReq(http,"https://api-sandbox.abnamro.com/v1/oauth/token",req,resp)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkPrivateKey::ckDispose(privkey)
CkJwt::ckDispose(jwt)
CkJsonObject::ckDispose(jsonHeader)
CkJsonObject::ckDispose(jsonPayload)
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
If CkHttpResponse::ckStatusCode(resp) <> 200
Debug CkHttpResponse::ckBodyStr(resp)
CkPrivateKey::ckDispose(privkey)
CkJwt::ckDispose(jwt)
CkJsonObject::ckDispose(jsonHeader)
CkJsonObject::ckDispose(jsonPayload)
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
; Get the JSON result:
; {
; "access_token": "{your access token}",
; "expires_in": "{duration of validity in seconds}",
; "scope": "{scope(s) for which the access token is valid}",
; "token_type": "{it is always Bearer}"
; }
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(resp))
Debug "access_token: " + CkJsonObject::ckStringOf(json,"access_token")
Debug "token_type: " + CkJsonObject::ckStringOf(json,"token_type")
Debug "expires_in: " + CkJsonObject::ckStringOf(json,"expires_in")
Debug "scope: " + CkJsonObject::ckStringOf(json,"scope")
CkPrivateKey::ckDispose(privkey)
CkJwt::ckDispose(jwt)
CkJsonObject::ckDispose(jsonHeader)
CkJsonObject::ckDispose(jsonPayload)
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndProcedure