Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Banco Inter OAuth2 Client CredentialsGenerate an OAuth2 access token needed to consume the Inter APIs. For more information, see https://developers.bancointer.com.br/reference/token-3
#include <C_CkHttpW.h> #include <C_CkCertW.h> #include <C_CkBinDataW.h> #include <C_CkPrivateKeyW.h> #include <C_CkHttpRequestW.h> #include <C_CkHttpResponseW.h> #include <C_CkJsonObjectW.h> void ChilkatSample(void) { HCkHttpW http; BOOL success; HCkCertW cert; HCkBinDataW bdPrivKey; HCkPrivateKeyW privKey; HCkHttpRequestW req; HCkHttpResponseW resp; HCkJsonObjectW jResp; int respStatusCode; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // First load the certificate and private key, and set as the HTTP object's client certificate. cert = CkCertW_Create(); success = CkCertW_LoadFromFile(cert,L"<nome arquivo certificado>.crt"); if (success == FALSE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkHttpW_Dispose(http); CkCertW_Dispose(cert); return; } bdPrivKey = CkBinDataW_Create(); success = CkBinDataW_LoadFile(bdPrivKey,L"<nome arquivo chave privada>.key"); if (success == FALSE) { wprintf(L"Failed to load <nome\n"); CkHttpW_Dispose(http); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPrivKey); return; } privKey = CkPrivateKeyW_Create(); success = CkPrivateKeyW_LoadAnyFormat(privKey,bdPrivKey,L""); if (success == FALSE) { wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey)); CkHttpW_Dispose(http); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPrivKey); CkPrivateKeyW_Dispose(privKey); return; } success = CkCertW_SetPrivateKey(cert,privKey); if (success == FALSE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkHttpW_Dispose(http); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPrivKey); CkPrivateKeyW_Dispose(privKey); return; } success = CkHttpW_SetSslClientCert(http,cert); if (success == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPrivKey); CkPrivateKeyW_Dispose(privKey); return; } req = CkHttpRequestW_Create(); CkHttpRequestW_putHttpVerb(req,L"POST"); CkHttpRequestW_putPath(req,L"/oauth/v2/token"); CkHttpRequestW_putContentType(req,L"application/x-www-form-urlencoded"); CkHttpRequestW_AddParam(req,L"grant_type",L"client_credentials"); // Requested scopes in OAuth2 are typically SPACE separated. CkHttpRequestW_AddParam(req,L"scope",L"boleto-cobranca.read boleto-cobranca.write"); CkHttpRequestW_AddHeader(req,L"accept",L"application/json"); resp = CkHttpW_PostUrlEncoded(http,L"https://cdpj.partners.bancointer.com.br/oauth/v2/token",req); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPrivKey); CkPrivateKeyW_Dispose(privKey); CkHttpRequestW_Dispose(req); return; } jResp = CkJsonObjectW_Create(); CkHttpResponseW_GetBodyJson(resp,jResp); CkJsonObjectW_putEmitCompact(jResp,FALSE); wprintf(L"Response Body:\n"); wprintf(L"%s\n",CkJsonObjectW_emit(jResp)); respStatusCode = CkHttpResponseW_getStatusCode(resp); wprintf(L"Response Status Code = %d\n",respStatusCode); if (respStatusCode >= 400) { wprintf(L"Response Header:\n"); wprintf(L"%s\n",CkHttpResponseW_header(resp)); wprintf(L"Failed.\n"); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPrivKey); CkPrivateKeyW_Dispose(privKey); CkHttpRequestW_Dispose(req); CkJsonObjectW_Dispose(jResp); return; } CkHttpResponseW_Dispose(resp); success = CkJsonObjectW_WriteFile(jResp,L"qa_data/tokens/banco_inter_client_credentials.json"); if (success == FALSE) { wprintf(L"Failed to save JSON access token file.\n"); CkHttpW_Dispose(http); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPrivKey); CkPrivateKeyW_Dispose(privKey); CkHttpRequestW_Dispose(req); CkJsonObjectW_Dispose(jResp); return; } wprintf(L"Success.\n"); CkHttpW_Dispose(http); CkCertW_Dispose(cert); CkBinDataW_Dispose(bdPrivKey); CkPrivateKeyW_Dispose(privKey); CkHttpRequestW_Dispose(req); CkJsonObjectW_Dispose(jResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.