Sample code for 30+ languages & platforms
Lua

Rabobank Refresh OAuth2 Access Token

See more OAuth2 Examples

Demonstrates how to refresh an expired Rabobank OAuth2 access token. (No interactive user interaction is required to refresh an access token.)

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 previously obtained an access token and saved the JSON to a file using this example:
-- Get Rabobank OAuth2 Access Token

local json = chilkat.JsonObject.new()
local success = json:load_file("qa_data/tokens/rabobank.json")
if not success then
    return
end

-- {
--   "token_type": "Bearer",
--   "access_token": "AAIkYTU0ZWM ... sxE0KL0HHWw",
--   "expires_in": 3600,
--   "consented_on": 1584641121,
--   "scope": "ais.balances.read ais.transactions.read-90days ais.transactions.read-history",
--   "refresh_token": "AAJXXSdN ... Uiblz9ILkxNBlytRGA",
--   "refresh_token_expires_in": 157784760,
--   "expires_on": "1584644722"
-- }

-- Refresh the access token by sending a POST like this:
--

-- POST /oauth2/token
-- Headers:
--     Content-Type: application/x-www-form-urlencoded
--     Authorization: Basic BASE64(CLIENT_ID + ":" + CLIENT_SECRET)
--
-- Body (x-www-form-urlencoded):
--     grant_type: refresh_token
--     refresh_token: REFRESH_TOKEN

local req = chilkat.HttpRequest.new()
req:add_param("grant_type", "refresh_token")
req:add_param("refresh_token", json:string_of("refresh_token"))

local http = chilkat.Http.new()

-- Causes the "Authorization: Basic BASE64(CLIENT_ID + ":" + CLIENT_SECRET)" header to be added..
http.basic_auth = true
http.login = "YOUR_APP_CLIENT_ID"
http.password = "YOUR_APP_CLIENT_SECRET"

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

local resp = chilkat.HttpResponse.new()
success = http:http_req("https://api-sandbox.rabobank.nl/openapi/sandbox/oauth2/token", req, resp)
if not success then
    print(http.last_error_text)
    return
end

-- Load the JSON response.
json:load(resp.body_str)
json.emit_compact = false

-- Show the JSON response.
print(json:emit())

print("Response status code: " .. resp.status_code)

-- If the response status code is not 200, then it's an error.
if resp.status_code ~= 200 then
    return
end

-- Save the refreshed access token JSON to a file for future requests.
local fac = chilkat.FileAccess.new()
fac:write_entire_text_file("qa_data/tokens/rabobank.json", json:emit(), "utf-8", false)