Sample code for 30+ languages & platforms
Rust

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 Rust Downloads

Rust

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

let http = chilkat::Http::new();

// 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.

let req = chilkat::HttpRequest::new();
req.add_param("grant_type", "client_credentials");
// Use your actual client ID and client secret...
req.add_param("client_id", "d0394a9f-0607-40de-a978-2d3eb8375b04");
req.add_param("client_secret", "6d62315e-d65a-4e41-9112-4195ea834edf");

req.add_header("posserial", "1234567899");
req.add_header("pososversion", "os");
req.add_header("posmodelframework", "1");
req.add_header("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"
req.set_http_verb("POST");
req.set_content_type("application/x-www-form-urlencoded");

let resp = chilkat::HttpResponse::new();
if http.http_req("https://invoicing.eta.gov.eg/connect/token", &req, &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);

let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);

println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());

let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
    println!("Response Header:");
    println!("{}", resp.header());
    println!("Failed.");
    return;
}

// If successful, the OAuth2 access token JSON looks like this:

// {
//   "token_type": "Bearer",
//   "access_token": "eyJraW......R2sbqrY",
//   "expires_in": "3600",
//   "scope": "..."
// }

let token_type = j_resp.string_of("token_type").unwrap_or_default();
let access_token = j_resp.string_of("access_token").unwrap_or_default();
let expires_in = j_resp.string_of("expires_in").unwrap_or_default();
let scope = j_resp.string_of("scope").unwrap_or_default();