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
(SQL Server) 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/
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- This example assumes the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @http int -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int -- 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. DECLARE @req int -- Use "Chilkat_9_5_0.HttpRequest" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT EXEC sp_OAMethod @req, 'AddParam', NULL, 'grant_type', 'client_credentials' -- Use your actual client ID and client secret... EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_id', 'd0394a9f-0607-40de-a978-2d3eb8375b04' EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_secret', '6d62315e-d65a-4e41-9112-4195ea834edf' EXEC sp_OAMethod @req, 'AddHeader', NULL, 'posserial', '1234567899' EXEC sp_OAMethod @req, 'AddHeader', NULL, 'pososversion', 'os' EXEC sp_OAMethod @req, 'AddHeader', NULL, 'posmodelframework', '1' EXEC sp_OAMethod @req, 'AddHeader', NULL, '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" DECLARE @resp int EXEC sp_OAMethod @http, 'PostUrlEncoded', @resp OUT, 'https://invoicing.eta.gov.eg/connect/token', @req EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 = 0 BEGIN EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @req RETURN END DECLARE @sbResponseBody int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody DECLARE @jResp int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody EXEC sp_OASetProperty @jResp, 'EmitCompact', 0 PRINT 'Response Body:' EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT PRINT @sTmp0 DECLARE @respStatusCode int EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT PRINT 'Response Status Code = ' + @respStatusCode IF @respStatusCode >= 400 BEGIN PRINT 'Response Header:' EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT PRINT @sTmp0 PRINT 'Failed.' EXEC @hr = sp_OADestroy @resp EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @req EXEC @hr = sp_OADestroy @sbResponseBody EXEC @hr = sp_OADestroy @jResp RETURN END EXEC @hr = sp_OADestroy @resp -- If successful, the OAuth2 access token JSON looks like this: -- { -- "token_type": "Bearer", -- "access_token": "eyJraW......R2sbqrY", -- "expires_in": "3600", -- "scope": "..." -- } DECLARE @token_type nvarchar(4000) EXEC sp_OAMethod @jResp, 'StringOf', @token_type OUT, 'token_type' DECLARE @access_token nvarchar(4000) EXEC sp_OAMethod @jResp, 'StringOf', @access_token OUT, 'access_token' DECLARE @expires_in nvarchar(4000) EXEC sp_OAMethod @jResp, 'StringOf', @expires_in OUT, 'expires_in' DECLARE @scope nvarchar(4000) EXEC sp_OAMethod @jResp, 'StringOf', @scope OUT, 'scope' EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @req EXEC @hr = sp_OADestroy @sbResponseBody EXEC @hr = sp_OADestroy @jResp END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.