PHP
PHP
Banco Inter OAuth2 Client Credentials
Generate an OAuth2 access token needed to consume the Inter APIs.Chilkat PHP Downloads
<?php
use Chilkat2\BinData;
use Chilkat2\Cert;
use Chilkat2\Http;
use Chilkat2\HttpRequest;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\PrivateKey;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
$http = new Http();
// First load the certificate and private key, and set as the HTTP object's client certificate.
$cert = new Cert();
try {
$cert->LoadFromFile('<nome arquivo certificado>.crt');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
$bdPrivKey = new BinData();
try {
$bdPrivKey->LoadFile('<nome arquivo chave privada>.key');
} catch (\Chilkat2\Exception $e) {
echo "Failed to load <nome\n";
exit;
}
$privKey = new PrivateKey();
try {
$privKey->LoadAnyFormat($bdPrivKey, '');
$cert->SetPrivateKey($privKey);
$http->SetSslClientCert($cert);
$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');
$resp = new HttpResponse();
$http->HttpReq('https://cdpj.partners.bancointer.com.br/oauth/v2/token', $req, $resp);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
$jResp = new JsonObject();
$resp->GetBodyJson($jResp);
$jResp->EmitCompact = false;
echo "Response Body:\n";
echo $jResp->Emit(), "\n";
$respStatusCode = $resp->StatusCode;
echo 'Response Status Code = ', $respStatusCode, "\n";
if ($respStatusCode >= 400) {
echo "Response Header:\n";
echo $resp->Header, "\n";
echo "Failed.\n";
exit;
}
try {
$jResp->WriteFile('qa_data/tokens/banco_inter_client_credentials.json');
} catch (\Chilkat2\Exception $e) {
echo "Failed to save JSON access token file.\n";
exit;
}
echo "Success.\n";