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
(C) Egyptian eReceipt OAuth2 Client CredentialsSee more Egypt eReceipt ExamplesGet an OAuth2 access token for the Egyptian eReceipt REST API using client credentials (no interactivity with a web browser required). For more information, see https://sdk.invoicing.eta.gov.eg/ereceiptapi/01-authenticate-pos/
#include <C_CkHttp.h> #include <C_CkHttpRequest.h> #include <C_CkHttpResponse.h> #include <C_CkStringBuilder.h> #include <C_CkJsonObject.h> void ChilkatSample(void) { HCkHttp http; BOOL success; HCkHttpRequest req; HCkHttpResponse resp; HCkStringBuilder sbResponseBody; HCkJsonObject jResp; int respStatusCode; const char *token_type; const char *access_token; const char *expires_in; const char *scope; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttp_Create(); // Note: Any provider of a REST API, such as the Egyptian government in this case, can make life // much easier for developers by providing one or more of the following in the API documentation: // // 1) A sample CURL statement for each API call. // 2) A Postman collection, or Swagger/OpenAPI specification file. // 3) A sample of a raw HTTP request and response for each API call. // // The sample CURL statements or raw HTTP request/responses do not need to comprehensively show all // possible options. Providing a sample allows one to quickly make a successful API call. // It also allows for code generation directly from the CURL, Postman collection, or raw request/response, // and it tends to answer all questions about the format/structure of a request that, suprisingly, // remain ambiguous or not obvious in other forms of documentation. req = CkHttpRequest_Create(); CkHttpRequest_AddParam(req,"grant_type","client_credentials"); // Use your actual client ID and client secret... CkHttpRequest_AddParam(req,"client_id","d0394a9f-0607-40de-a978-2d3eb8375b04"); CkHttpRequest_AddParam(req,"client_secret","6d62315e-d65a-4e41-9112-4195ea834edf"); CkHttpRequest_AddHeader(req,"posserial","1234567899"); CkHttpRequest_AddHeader(req,"pososversion","os"); CkHttpRequest_AddHeader(req,"posmodelframework","1"); CkHttpRequest_AddHeader(req,"presharedkey","03ac674216f3e1..."); // When writing this example, the documentation at https://sdk.invoicing.eta.gov.eg/ereceiptapi/01-authenticate-pos/ // shows us the HTTP verb and path (POST /connect/token), however, // we don't see the actual domain where the request is to be sent. // What are the endpoints??? // It took some searching, but we found some endpoints here: https://sdk.invoicing.eta.gov.eg/faq/ // It's not immediately apparent which endpoint is to be used with a given API call. // Why not just include the endpoint in the documentation for each REST API call? // Endpoints are literally the #1 thing that needs to be known. // They can't just be buried in a FAQ. They should be up-front and obvious. // // So.. we're guessing the endpoint is likely "https://invoicing.eta.gov.eg/connect/token" resp = CkHttp_PostUrlEncoded(http,"https://invoicing.eta.gov.eg/connect/token",req); if (CkHttp_getLastMethodSuccess(http) == FALSE) { printf("%s\n",CkHttp_lastErrorText(http)); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); return; } sbResponseBody = CkStringBuilder_Create(); CkHttpResponse_GetBodySb(resp,sbResponseBody); jResp = CkJsonObject_Create(); CkJsonObject_LoadSb(jResp,sbResponseBody); CkJsonObject_putEmitCompact(jResp,FALSE); printf("Response Body:\n"); printf("%s\n",CkJsonObject_emit(jResp)); respStatusCode = CkHttpResponse_getStatusCode(resp); printf("Response Status Code = %d\n",respStatusCode); if (respStatusCode >= 400) { printf("Response Header:\n"); printf("%s\n",CkHttpResponse_header(resp)); printf("Failed.\n"); CkHttpResponse_Dispose(resp); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkStringBuilder_Dispose(sbResponseBody); CkJsonObject_Dispose(jResp); return; } CkHttpResponse_Dispose(resp); // If successful, the OAuth2 access token JSON looks like this: // { // "token_type": "Bearer", // "access_token": "eyJraW......R2sbqrY", // "expires_in": "3600", // "scope": "..." // } token_type = CkJsonObject_stringOf(jResp,"token_type"); access_token = CkJsonObject_stringOf(jResp,"access_token"); expires_in = CkJsonObject_stringOf(jResp,"expires_in"); scope = CkJsonObject_stringOf(jResp,"scope"); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkStringBuilder_Dispose(sbResponseBody); CkJsonObject_Dispose(jResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.