Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Banco Inter OAuth2 Client Credentials
Generate an OAuth2 access token needed to consume the Inter APIs.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.HttpResponse,
Chilkat.JsonObject,
Chilkat.HttpRequest,
Chilkat.BinData,
Chilkat.PrivateKey,
Chilkat.Cert,
Chilkat.Http;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
cert: TCert;
bdPrivKey: TBinData;
privKey: TPrivateKey;
req: THttpRequest;
resp: THttpResponse;
jResp: TJsonObject;
respStatusCode: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := THttp.Create;
// First load the certificate and private key, and set as the HTTP object's client certificate.
cert := TCert.Create;
success := cert.LoadFromFile('<nome arquivo certificado>.crt');
if (success = False) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
bdPrivKey := TBinData.Create;
success := bdPrivKey.LoadFile('<nome arquivo chave privada>.key');
if (success = False) then
begin
WriteLn('Failed to load <nome');
Exit;
end;
privKey := TPrivateKey.Create;
success := privKey.LoadAnyFormat(bdPrivKey,'');
if (success = False) then
begin
WriteLn(privKey.LastErrorText);
Exit;
end;
success := cert.SetPrivateKey(privKey);
if (success = False) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
success := http.SetSslClientCert(cert);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
req := THttpRequest.Create;
req.HttpVerb := 'POST';
req.Path := '/oauth/v2/token';
req.ContentType := 'application/x-www-form-urlencoded';
req.AddParam('grant_type','client_credentials');
// Requested scopes in OAuth2 are typically SPACE separated.
req.AddParam('scope','boleto-cobranca.read boleto-cobranca.write');
req.AddHeader('accept','application/json');
resp := THttpResponse.Create;
success := http.HttpReq('https://cdpj.partners.bancointer.com.br/oauth/v2/token',req,resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
jResp := TJsonObject.Create;
resp.GetBodyJson(jResp);
jResp.EmitCompact := False;
WriteLn('Response Body:');
WriteLn(jResp.Emit());
respStatusCode := resp.StatusCode;
WriteLn('Response Status Code = ' + respStatusCode);
if (respStatusCode >= 400) then
begin
WriteLn('Response Header:');
WriteLn(resp.Header);
WriteLn('Failed.');
Exit;
end;
success := jResp.WriteFile('qa_data/tokens/banco_inter_client_credentials.json');
if (success = False) then
begin
WriteLn('Failed to save JSON access token file.');
Exit;
end;
WriteLn('Success.');
http.Free;
cert.Free;
bdPrivKey.Free;
privKey.Free;
req.Free;
resp.Free;
jResp.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.