Sample code for 30+ languages & platforms
Dart

Banco Inter OAuth2 Client Credentials

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

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final http = CkHttp();

  // First load the certificate and private key, and set as the HTTP object's client certificate.
  final cert = CkCert();
  try {
    cert.loadFromFile('<nome arquivo certificado>.crt');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final bdPrivKey = CkBinData();
  try {
    bdPrivKey.loadFile('<nome arquivo chave privada>.key');
  } on ChilkatException {
    print('Failed to load <nome');
    return;
  }

  final privKey = CkPrivateKey();
  try {
    privKey.loadAnyFormat(bdPrivKey, '');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    cert.setPrivateKey(privKey);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    http.setSslClientCert(cert);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final req = CkHttpRequest();
  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');

  final resp = CkHttpResponse();
  try {
    http.httpReq('https://cdpj.partners.bancointer.com.br/oauth/v2/token', req, resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final jResp = CkJsonObject();
  resp.getBodyJson(jResp);
  jResp.emitCompact = false;

  print('Response Body:');
  print(jResp.emit());

  final respStatusCode = resp.statusCode;
  print('Response Status Code = $respStatusCode');
  if (respStatusCode >= 400) {
    print('Response Header:');
    print(resp.header);
    print('Failed.');
    return;
  }

  try {
    jResp.writeFile('qa_data/tokens/banco_inter_client_credentials.json');
  } on ChilkatException {
    print('Failed to save JSON access token file.');
    return;
  }

  print('Success.');
}