Lua
Lua
citi Developer OAuth2 Client Credentials Grant
See more OAuth2 Examples
Get access token for your application credentials. You can use this for citi APIs which do not require customer credential verification and consent (e.g. Onboarding).Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- Implements the following CURL command:
-- curl --request POST \
-- --url https://sandbox.apihub.citi.com/gcb/api/clientCredentials/oauth2/token/us/gcb \
-- --header 'accept: application/json' \
-- --user client-id:client-secret \
-- --header 'content-type: application/x-www-form-urlencoded' \
-- --data 'grant_type=client_credentials&scope=%2Fapi'
http.login = "client-id"
http.password = "client-secret"
local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.path = "/gcb/api/clientCredentials/oauth2/token/us/gcb"
req.content_type = "application/x-www-form-urlencoded"
req:add_param("grant_type", "client_credentials")
req:add_param("scope", "/api")
req:add_header("accept", "application/json")
local resp = chilkat.HttpResponse.new()
local success = http:http_req("https://sandbox.apihub.citi.com/gcb/api/clientCredentials/oauth2/token/us/gcb", req, resp)
if not success then
print(http.last_error_text)
return
end
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(resp.header)
print("Failed.")
return
end
success = jResp:write_file("qa_data/tokens/citi_client_credentials.json")
if not success then
print("Failed to save JSON access token file.")
return
end
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "token_type": "bearer",
-- "access_token": "AAIkMjdh ... 3fsWb7zJ0s",
-- "expires_in": 1800,
-- "consented_on": 1584817860,
-- "scope": "/api"
-- }
-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
local token_type = jResp:string_of("token_type")
local access_token = jResp:string_of("access_token")
local expires_in = jResp:int_of("expires_in")
local consented_on = jResp:int_of("consented_on")
local scope = jResp:string_of("scope")