Java
Chilkat2 Java
Banco Inter OAuth2 Client Credentials
Generate an OAuth2 access token needed to consume the Inter APIs.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.PrivateKey;
public class ChilkatExample {
public static void main(String[] args) {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
// First load the certificate and private key, and set as the HTTP object's client certificate.
Cert cert = new Cert();
try {
cert.loadFromFile("<nome arquivo certificado>.crt");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
BinData bdPrivKey = new BinData();
try {
bdPrivKey.loadFile("<nome arquivo chave privada>.key");
} catch (ChilkatException e) {
System.out.println("Failed to load <nome");
return;
}
PrivateKey privKey = new PrivateKey();
try {
privKey.loadAnyFormat(bdPrivKey, "");
cert.setPrivateKey(privKey);
http.setSslClientCert(cert);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
HttpRequest req = new HttpRequest();
req.setHttpVerb("POST");
req.setPath("/oauth/v2/token");
req.setContentType("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");
HttpResponse resp = new HttpResponse();
try {
http.httpReq("https://cdpj.partners.bancointer.com.br/oauth/v2/token", req, resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject jResp = new JsonObject();
resp.getBodyJson(jResp);
jResp.setEmitCompact(false);
System.out.println("Response Body:");
System.out.println(jResp.emit());
int respStatusCode = resp.getStatusCode();
System.out.println("Response Status Code = " + respStatusCode);
if (respStatusCode >= 400) {
System.out.println("Response Header:");
System.out.println(resp.getHeader());
System.out.println("Failed.");
return;
}
try {
jResp.writeFile("qa_data/tokens/banco_inter_client_credentials.json");
} catch (ChilkatException e) {
System.out.println("Failed to save JSON access token file.");
return;
}
System.out.println("Success.");
}
}