AutoIt
AutoIt
Banco Inter OAuth2 Client Credentials
Generate an OAuth2 access token needed to consume the Inter APIs.Chilkat AutoIt Downloads
Local $bSuccess = False
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
$oHttp = ObjCreate("Chilkat.Http")
; First load the certificate and private key, and set as the HTTP object's client certificate.
$oCert = ObjCreate("Chilkat.Cert")
$bSuccess = $oCert.LoadFromFile("<nome arquivo certificado>.crt")
If ($bSuccess = False) Then
ConsoleWrite($oCert.LastErrorText & @CRLF)
Exit
EndIf
$oBdPrivKey = ObjCreate("Chilkat.BinData")
$bSuccess = $oBdPrivKey.LoadFile("<nome arquivo chave privada>.key")
If ($bSuccess = False) Then
ConsoleWrite("Failed to load <nome" & @CRLF)
Exit
EndIf
$oPrivKey = ObjCreate("Chilkat.PrivateKey")
$bSuccess = $oPrivKey.LoadAnyFormat($oBdPrivKey,"")
If ($bSuccess = False) Then
ConsoleWrite($oPrivKey.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oCert.SetPrivateKey($oPrivKey)
If ($bSuccess = False) Then
ConsoleWrite($oCert.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oHttp.SetSslClientCert($oCert)
If ($bSuccess = False) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Exit
EndIf
$oReq = ObjCreate("Chilkat.HttpRequest")
$oReq.HttpVerb = "POST"
$oReq.Path = "/oauth/v2/token"
$oReq.ContentType = "application/x-www-form-urlencoded"
$oReq.AddParam "grant_type","client_credentials"
; Requested scopes in OAuth2 are typically SPACE separated.
$oReq.AddParam "scope","boleto-cobranca.read boleto-cobranca.write"
$oReq.AddHeader "accept","application/json"
$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpReq("https://cdpj.partners.bancointer.com.br/oauth/v2/token",$oReq,$oResp)
If ($bSuccess = False) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Exit
EndIf
$oJResp = ObjCreate("Chilkat.JsonObject")
$oResp.GetBodyJson($oJResp)
$oJResp.EmitCompact = False
ConsoleWrite("Response Body:" & @CRLF)
ConsoleWrite($oJResp.Emit() & @CRLF)
Local $iRespStatusCode = $oResp.StatusCode
ConsoleWrite("Response Status Code = " & $iRespStatusCode & @CRLF)
If ($iRespStatusCode >= 400) Then
ConsoleWrite("Response Header:" & @CRLF)
ConsoleWrite($oResp.Header & @CRLF)
ConsoleWrite("Failed." & @CRLF)
Exit
EndIf
$bSuccess = $oJResp.WriteFile("qa_data/tokens/banco_inter_client_credentials.json")
If ($bSuccess = False) Then
ConsoleWrite("Failed to save JSON access token file." & @CRLF)
Exit
EndIf
ConsoleWrite("Success." & @CRLF)