Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- 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.
local privkey = chilkat.PrivateKey.new()

local success = privkey:load_pem_file("qa_data/pem/abnAmroPrivateKey.pem")
if not success then
    print(privkey.last_error_text)
    return
end

-- Create the JWT.
local jwt = chilkat.Jwt.new()

-- Create the header:
-- {
--     "typ": "JWT",
--     "alg": "RS256"
-- }
local jsonHeader = chilkat.JsonObject.new()
jsonHeader:update_string("typ", "JWT")
jsonHeader:update_string("alg", "RS256")

-- Create the payload:
-- {
--     "nbf": 1499947668,
--     "exp": 1499948668,
--     "iss": "me",
--     "sub": "anApiKey",
--     "aud": "https://auth-sandbox.abnamro.com/oauth/token"
-- }
local jsonPayload = chilkat.JsonObject.new()

local curDateTime = jwt:gen_numeric_date(0)

-- Set the "not process before" timestamp to now.
success = jsonPayload:add_int_at(-1, "nbf", curDateTime)

-- Set the timestamp defining an expiration time (end time) for the token
-- to be now + 1 hour (3600 seconds)
success = jsonPayload:add_int_at(-1, "exp", curDateTime + 3600)

jsonPayload:update_string("iss", "me")
jsonPayload:update_string("sub", "anApiKey")
jsonPayload:update_string("aud", "https://auth-sandbox.abnamro.com/oauth/token")

-- Produce the smallest possible JWT:
jwt.auto_compact = true

local jwtStr = jwt:create_jwt_pk(jsonHeader:emit(), jsonPayload:emit(), privkey)
if not jwt.last_method_success then
    print(jwt.last_error_text)
    return
end

local http = chilkat.Http.new()

local req = chilkat.HttpRequest.new()
req:add_param("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer")
req:add_param("grant_type", "client_credentials")
req:add_param("client_assertion", jwtStr)
req:add_param("scope", "tikkie")

req.http_verb = "POST"
req.content_type = "application/x-www-form-urlencoded"

local resp = chilkat.HttpResponse.new()
success = http:http_req("https://api-sandbox.abnamro.com/v1/oauth/token", req, resp)
if not success then
    print(http.last_error_text)
    return
end

if resp.status_code ~= 200 then
    print(resp.body_str)
    return
end

-- 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}"
-- }
local json = chilkat.JsonObject.new()
json:load(resp.body_str)
print("access_token: " .. json:string_of("access_token"))
print("token_type: " .. json:string_of("token_type"))
print("expires_in: " .. json:string_of("expires_in"))
print("scope: " .. json:string_of("scope"))