DataFlex
DataFlex
Egyptian eReceipt OAuth2 Client Credentials
See more Egypt eReceipt Examples
Get an OAuth2 access token for the Egyptian eReceipt REST API using client credentials (no interactivity with a web browser required).Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Variant vReq
Handle hoReq
Variant vResp
Handle hoResp
Variant vSbResponseBody
Handle hoSbResponseBody
Handle hoJResp
Integer iRespStatusCode
String sToken_type
String sAccess_token
String sExpires_in
String sScope
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
// 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.
Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
If (Not(IsComObjectCreated(hoReq))) Begin
Send CreateComObject of hoReq
End
Send ComAddParam To hoReq "grant_type" "client_credentials"
// Use your actual client ID and client secret...
Send ComAddParam To hoReq "client_id" "d0394a9f-0607-40de-a978-2d3eb8375b04"
Send ComAddParam To hoReq "client_secret" "6d62315e-d65a-4e41-9112-4195ea834edf"
Send ComAddHeader To hoReq "posserial" "1234567899"
Send ComAddHeader To hoReq "pososversion" "os"
Send ComAddHeader To hoReq "posmodelframework" "1"
Send ComAddHeader To hoReq "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"
Set ComHttpVerb Of hoReq To "POST"
Set ComContentType Of hoReq To "application/x-www-form-urlencoded"
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://invoicing.eta.gov.eg/connect/token" vReq vResp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
Send CreateComObject of hoSbResponseBody
End
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
If (Not(IsComObjectCreated(hoJResp))) Begin
Send CreateComObject of hoJResp
End
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComLoadSb Of hoJResp vSbResponseBody 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
// If successful, the OAuth2 access token JSON looks like this:
// {
// "token_type": "Bearer",
// "access_token": "eyJraW......R2sbqrY",
// "expires_in": "3600",
// "scope": "..."
// }
Get ComStringOf Of hoJResp "token_type" To sToken_type
Get ComStringOf Of hoJResp "access_token" To sAccess_token
Get ComStringOf Of hoJResp "expires_in" To sExpires_in
Get ComStringOf Of hoJResp "scope" To sScope
End_Procedure