Sample code for 30+ languages & platforms
DataFlex

Banco Inter OAuth2 Client Credentials

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

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vCert
    Handle hoCert
    Variant vBdPrivKey
    Handle hoBdPrivKey
    Variant vPrivKey
    Handle hoPrivKey
    Variant vReq
    Handle hoReq
    Variant vResp
    Handle hoResp
    Variant vJResp
    Handle hoJResp
    Integer iRespStatusCode
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // First load the certificate and private key, and set as the HTTP object's client certificate.
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadFromFile Of hoCert "<nome arquivo certificado>.crt" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatBinData)) To hoBdPrivKey
    If (Not(IsComObjectCreated(hoBdPrivKey))) Begin
        Send CreateComObject of hoBdPrivKey
    End
    Get ComLoadFile Of hoBdPrivKey "<nome arquivo chave privada>.key" To iSuccess
    If (iSuccess = False) Begin
        Showln "Failed to load <nome"
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End
    Get pvComObject of hoBdPrivKey to vBdPrivKey
    Get ComLoadAnyFormat Of hoPrivKey vBdPrivKey "" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPrivKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoPrivKey to vPrivKey
    Get ComSetPrivateKey Of hoCert vPrivKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoCert to vCert
    Get ComSetSslClientCert Of hoHttp vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End
    Set ComHttpVerb Of hoReq To "POST"
    Set ComPath Of hoReq To "/oauth/v2/token"
    Set ComContentType Of hoReq To "application/x-www-form-urlencoded"
    Send ComAddParam To hoReq "grant_type" "client_credentials"
    // Requested scopes in OAuth2 are typically SPACE separated.
    Send ComAddParam To hoReq "scope" "boleto-cobranca.read boleto-cobranca.write"
    Send ComAddHeader To hoReq "accept" "application/json"

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoReq to vReq
    Get pvComObject of hoResp to vResp
    Get ComHttpReq Of hoHttp "https://cdpj.partners.bancointer.com.br/oauth/v2/token" vReq vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
    If (Not(IsComObjectCreated(hoJResp))) Begin
        Send CreateComObject of hoJResp
    End
    Get pvComObject of hoJResp to vJResp
    Get ComGetBodyJson Of hoResp vJResp To iSuccess
    Set ComEmitCompact Of hoJResp To False

    Showln "Response Body:"
    Get ComEmit Of hoJResp To sTemp1
    Showln sTemp1

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Header:"
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Procedure_Return
    End

    Get ComWriteFile Of hoJResp "qa_data/tokens/banco_inter_client_credentials.json" To iSuccess
    If (iSuccess = False) Begin
        Showln "Failed to save JSON access token file."
        Procedure_Return
    End

    Showln "Success."


End_Procedure