Sample code for 30+ languages & platforms
React Native

Banco Inter OAuth2 Client Credentials

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

Chilkat React Native Downloads

React Native
import { BinData, Cert, Http, HttpRequest, HttpResponse, JsonObject, PrivateKey } from '@chilkat/react-native'

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

  const http = new Http();

  // First load the certificate and private key, and set as the HTTP object's client certificate.
  const cert = new Cert();
  try {
    cert.loadFromFile('<nome arquivo certificado>.crt');
  } catch {
    console.log(cert.lastErrorText);
    return;
  }

  const bdPrivKey = new BinData();
  try {
    bdPrivKey.loadFile('<nome arquivo chave privada>.key');
  } catch {
    console.log('Failed to load <nome');
    return;
  }

  const privKey = new PrivateKey();
  try {
    privKey.loadAnyFormat(bdPrivKey, '');
  } catch {
    console.log(privKey.lastErrorText);
    return;
  }

  try {
    cert.setPrivateKey(privKey);
  } catch {
    console.log(cert.lastErrorText);
    return;
  }

  try {
    http.setSslClientCert(cert);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const req = new HttpRequest();
  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');

  const resp = new HttpResponse();
  try {
    await http.httpReqAsync('https://cdpj.partners.bancointer.com.br/oauth/v2/token', req, resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const jResp = new JsonObject();
  resp.getBodyJson(jResp);
  jResp.emitCompact = false;

  console.log('Response Body:');
  console.log(jResp.emit());

  const respStatusCode = resp.statusCode;
  console.log(`Response Status Code = ${respStatusCode}`);
  if (respStatusCode >= 400) {
    console.log('Response Header:');
    console.log(resp.header);
    console.log('Failed.');
    return;
  }

  try {
    jResp.writeFile('qa_data/tokens/banco_inter_client_credentials.json');
  } catch {
    console.log('Failed to save JSON access token file.');
    return;
  }

  console.log('Success.');
}