Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Get Access Token using a Pre-Created JSON Web TokenDemonstrates how to get an access token using a pre-created JSON Web Token (JWT). For more information, see https://developer.abnamro.com/get-started#headingFive
IncludeFile "CkJsonObject.pb" IncludeFile "CkJwt.pb" IncludeFile "CkPrivateKey.pb" IncludeFile "CkHttp.pb" IncludeFile "CkHttpRequest.pb" IncludeFile "CkHttpResponse.pb" Procedure ChilkatExample() ; 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.i = 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) <> 1 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") resp.i = CkHttp::ckPostUrlEncoded(http,"https://api-sandbox.abnamro.com/v1/oauth/token",req) If CkHttp::ckLastMethodSuccess(http) = 0 Debug CkHttp::ckLastErrorText(http) CkPrivateKey::ckDispose(privkey) CkJwt::ckDispose(jwt) CkJsonObject::ckDispose(jsonHeader) CkJsonObject::ckDispose(jsonPayload) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) ProcedureReturn EndIf If CkHttpResponse::ckStatusCode(resp) <> 200 Debug CkHttpResponse::ckBodyStr(resp) CkHttpResponse::ckDispose(resp) CkPrivateKey::ckDispose(privkey) CkJwt::ckDispose(jwt) CkJsonObject::ckDispose(jsonHeader) CkJsonObject::ckDispose(jsonPayload) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) 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") CkHttpResponse::ckDispose(resp) CkPrivateKey::ckDispose(privkey) CkJwt::ckDispose(jwt) CkJsonObject::ckDispose(jsonHeader) CkJsonObject::ckDispose(jsonPayload) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkJsonObject::ckDispose(json) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.