Delphi DLL
Delphi DLL
Banco Inter OAuth2 Client Credentials
Generate an OAuth2 access token needed to consume the Inter APIs.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, HttpResponse, JsonObject, HttpRequest, BinData, PrivateKey, Cert, Http;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
http: HCkHttp;
cert: HCkCert;
bdPrivKey: HCkBinData;
privKey: HCkPrivateKey;
req: HCkHttpRequest;
resp: HCkHttpResponse;
jResp: HCkJsonObject;
respStatusCode: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := CkHttp_Create();
// First load the certificate and private key, and set as the HTTP object's client certificate.
cert := CkCert_Create();
success := CkCert_LoadFromFile(cert,'<nome arquivo certificado>.crt');
if (success = False) then
begin
Memo1.Lines.Add(CkCert__lastErrorText(cert));
Exit;
end;
bdPrivKey := CkBinData_Create();
success := CkBinData_LoadFile(bdPrivKey,'<nome arquivo chave privada>.key');
if (success = False) then
begin
Memo1.Lines.Add('Failed to load <nome');
Exit;
end;
privKey := CkPrivateKey_Create();
success := CkPrivateKey_LoadAnyFormat(privKey,bdPrivKey,'');
if (success = False) then
begin
Memo1.Lines.Add(CkPrivateKey__lastErrorText(privKey));
Exit;
end;
success := CkCert_SetPrivateKey(cert,privKey);
if (success = False) then
begin
Memo1.Lines.Add(CkCert__lastErrorText(cert));
Exit;
end;
success := CkHttp_SetSslClientCert(http,cert);
if (success = False) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
req := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(req,'POST');
CkHttpRequest_putPath(req,'/oauth/v2/token');
CkHttpRequest_putContentType(req,'application/x-www-form-urlencoded');
CkHttpRequest_AddParam(req,'grant_type','client_credentials');
// Requested scopes in OAuth2 are typically SPACE separated.
CkHttpRequest_AddParam(req,'scope','boleto-cobranca.read boleto-cobranca.write');
CkHttpRequest_AddHeader(req,'accept','application/json');
resp := CkHttpResponse_Create();
success := CkHttp_HttpReq(http,'https://cdpj.partners.bancointer.com.br/oauth/v2/token',req,resp);
if (success = False) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
jResp := CkJsonObject_Create();
CkHttpResponse_GetBodyJson(resp,jResp);
CkJsonObject_putEmitCompact(jResp,False);
Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonObject__emit(jResp));
respStatusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
begin
Memo1.Lines.Add('Response Header:');
Memo1.Lines.Add(CkHttpResponse__header(resp));
Memo1.Lines.Add('Failed.');
Exit;
end;
success := CkJsonObject_WriteFile(jResp,'qa_data/tokens/banco_inter_client_credentials.json');
if (success = False) then
begin
Memo1.Lines.Add('Failed to save JSON access token file.');
Exit;
end;
Memo1.Lines.Add('Success.');
CkHttp_Dispose(http);
CkCert_Dispose(cert);
CkBinData_Dispose(bdPrivKey);
CkPrivateKey_Dispose(privKey);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(jResp);
end;