Sample code for 30+ languages & platforms
Swift

Banco Inter OAuth2 Client Credentials

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

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let http = CkoHttp()!

    // First load the certificate and private key, and set as the HTTP object's client certificate.
    let cert = CkoCert()!
    success = cert.load(fromFile: "<nome arquivo certificado>.crt")
    if success == false {
        print("\(cert.lastErrorText!)")
        return
    }

    let bdPrivKey = CkoBinData()!
    success = bdPrivKey.loadFile(path: "<nome arquivo chave privada>.key")
    if success == false {
        print("Failed to load <nome")
        return
    }

    let privKey = CkoPrivateKey()!
    success = privKey.loadAnyFormat(privKeyData: bdPrivKey, password: "")
    if success == false {
        print("\(privKey.lastErrorText!)")
        return
    }

    success = cert.setPrivateKey(privKey: privKey)
    if success == false {
        print("\(cert.lastErrorText!)")
        return
    }

    success = http.setSslClientCert(cert: cert)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    let req = CkoHttpRequest()!
    req.httpVerb = "POST"
    req.path = "/oauth/v2/token"
    req.contentType = "application/x-www-form-urlencoded"
    req.addParam(name: "grant_type", value: "client_credentials")
    // Requested scopes in OAuth2 are typically SPACE separated.
    req.addParam(name: "scope", value: "boleto-cobranca.read boleto-cobranca.write")
    req.addHeader(name: "accept", value: "application/json")

    let resp = CkoHttpResponse()!
    success = http.httpReq(url: "https://cdpj.partners.bancointer.com.br/oauth/v2/token", request: req, response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    let jResp = CkoJsonObject()!
    resp.getBodyJson(json: jResp)
    jResp.emitCompact = false

    print("Response Body:")
    print("\(jResp.emit()!)")

    var respStatusCode: Int = resp.statusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp.header!)")
        print("Failed.")
        return
    }

    success = jResp.writeFile(path: "qa_data/tokens/banco_inter_client_credentials.json")
    if success == false {
        print("Failed to save JSON access token file.")
        return
    }

    print("Success.")

}