Sample code for 30+ languages & platforms
Lua

Banco Inter OAuth2 Client Credentials

Generate an OAuth2 access token needed to consume the Inter APIs.

Chilkat Lua Downloads

Lua
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()

-- First load the certificate and private key, and set as the HTTP object's client certificate.
local cert = chilkat.Cert.new()
local success = cert:load_from_file("<nome arquivo certificado>.crt")
if not success then
    print(cert.last_error_text)
    return
end

local bdPrivKey = chilkat.BinData.new()
success = bdPrivKey:load_file("<nome arquivo chave privada>.key")
if not success then
    print("Failed to load <nome")
    return
end

local privKey = chilkat.PrivateKey.new()
success = privKey:load_any_format(bdPrivKey, "")
if not success then
    print(privKey.last_error_text)
    return
end

success = cert:set_private_key(privKey)
if not success then
    print(cert.last_error_text)
    return
end

success = http:set_ssl_client_cert(cert)
if not success then
    print(http.last_error_text)
    return
end

local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.path = "/oauth/v2/token"
req.content_type = "application/x-www-form-urlencoded"
req:add_param("grant_type", "client_credentials")
-- Requested scopes in OAuth2 are typically SPACE separated.
req:add_param("scope", "boleto-cobranca.read boleto-cobranca.write")
req:add_header("accept", "application/json")

local resp = chilkat.HttpResponse.new()
success = http:http_req("https://cdpj.partners.bancointer.com.br/oauth/v2/token", req, resp)
if not success then
    print(http.last_error_text)
    return
end

local jResp = chilkat.JsonObject.new()
resp:get_body_json(jResp)
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/banco_inter_client_credentials.json")
if not success then
    print("Failed to save JSON access token file.")
    return
end

print("Success.")