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) ShippingEasy.com Calculate Signature for API AuthenticationDemonstrates how to calculate the shippingeasy.com API signature for authenticating requests.
-- 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 requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. -- -- First, concatenate these into a plaintext string using the following order: -- -- Capitilized method of the request. E.g. "POST" -- The URI path -- The query parameters sorted alphabetically and concatenated together into a URL friendly format: param1=ABC¶m2=XYZ -- The request body as a string if one exists -- All parts are then concatenated together with an ampersand. The result resembles something like this: -- -- "POST&/partners/api/accounts&api_key=f9a7c8ebdfd34beaf260d9b0296c7059&api_timestamp=1401803554&{ ... request body ... }" DECLARE @success int DECLARE @sbStringToSign int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbStringToSign OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @httpVerb nvarchar(4000) SELECT @httpVerb = 'POST' DECLARE @uriPath nvarchar(4000) SELECT @uriPath = '/partners/api/accounts' DECLARE @queryParamsStr nvarchar(4000) SELECT @queryParamsStr = 'api_key=YOUR_API_KEY&api_timestamp=UNIX_EPOCH_TIMESTAMP' -- Build the following JSON that will be the body of the request: -- { -- "account": { -- "first_name": "Coralie", -- "last_name": "Waelchi", -- "company_name": "Hegmann, Cremin and Bradtke", -- "email": "se_greg_6d477b1e59e8ff24abadfb59d3a2de3e@shippingeasy.com", -- "phone_number": "1-381-014-3358", -- "address": "2476 Flo Inlet", -- "address2": "", -- "state": "SC", -- "city": "North Dennis", -- "postal_code": "29805", -- "country": "USA", -- "password": "abc123", -- "subscription_plan_code": "starter" -- } -- } DECLARE @json int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.first_name', 'Coralie' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.last_name', 'Waelchi' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.company_name', 'Hegmann, Cremin and Bradtke' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.email', 'se_greg_6d477b1e59e8ff24abadfb59d3a2de3e@shippingeasy.com' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.phone_number', '1-381-014-3358' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.address', '2476 Flo Inlet' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.address2', '' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.state', 'SC' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.city', 'North Dennis' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.postal_code', '29805' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.country', 'USA' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.password', 'abc123' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'account.subscription_plan_code', 'starter' EXEC sp_OASetProperty @json, 'EmitCompact', 0 EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT PRINT @sTmp0 -- First, let's get the current date/time in the Unix Epoch Timestamp format (which is just an integer) DECLARE @dt int -- Use "Chilkat_9_5_0.CkDateTime" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dt OUT EXEC sp_OAMethod @dt, 'SetFromCurrentSystemTime', @success OUT -- Get the UTC time. DECLARE @bLocalTime int SELECT @bLocalTime = 0 DECLARE @unixEpochTimestamp nvarchar(4000) EXEC sp_OAMethod @dt, 'GetAsUnixTimeStr', @unixEpochTimestamp OUT, @bLocalTime -- Build the string to sign: EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, @httpVerb EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, '&' EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, @uriPath EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, '&' EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, @queryParamsStr EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, '&' -- Make sure to send the JSON body of a request in compact form.. EXEC sp_OASetProperty @json, 'EmitCompact', 1 EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, @sTmp0 -- Use your API key here: DECLARE @your_api_key nvarchar(4000) SELECT @your_api_key = 'f9a7c8ebdfd34beaf260d9b0296c7059' DECLARE @numReplaced int EXEC sp_OAMethod @sbStringToSign, 'Replace', @numReplaced OUT, 'YOUR_API_KEY', @your_api_key EXEC sp_OAMethod @sbStringToSign, 'Replace', @numReplaced OUT, 'UNIX_EPOCH_TIMESTAMP', @unixEpochTimestamp -- Do the HMAC-SHA256 with your API secret: DECLARE @your_api_secret nvarchar(4000) SELECT @your_api_secret = 'ea210785fa4656af03c2e4ffcc2e7b5fc19f1fba577d137905cc97e74e1df53d' DECLARE @crypt int -- Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT EXEC sp_OASetProperty @crypt, 'MacAlgorithm', 'hmac' EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hexlower' EXEC sp_OAMethod @crypt, 'SetMacKeyString', @success OUT, @your_api_secret EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'sha256' DECLARE @api_signature nvarchar(4000) EXEC sp_OAMethod @sbStringToSign, 'GetAsString', @sTmp0 OUT EXEC sp_OAMethod @crypt, 'MacStringENC', @api_signature OUT, @sTmp0 PRINT 'api_signature: ' + @api_signature -- -------------------------------------------------------------------- -- Here's an example showing how to use the signature in a request: -- Build a new string-to-sign and create a new api_signature for the actual request we'll be sending... EXEC sp_OAMethod @sbStringToSign, 'Clear', NULL EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, 'GET' EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, '&' EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, '/app.shippingeasy.com/api/orders' EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, '&' EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, @queryParamsStr EXEC sp_OAMethod @sbStringToSign, 'Append', @success OUT, '&' -- There is no body for a GET request. EXEC sp_OAMethod @sbStringToSign, 'GetAsString', @sTmp0 OUT EXEC sp_OAMethod @crypt, 'MacStringENC', @api_signature OUT, @sTmp0 DECLARE @http int -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT DECLARE @queryParams int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @queryParams OUT EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'api_signature', @api_signature EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'api_timestamp', @unixEpochTimestamp EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'api_key', @your_api_key DECLARE @resp int EXEC sp_OAMethod @http, 'QuickRequestParams', @resp OUT, 'GET', 'https://app.shippingeasy.com/api/orders', @queryParams EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 = 0 BEGIN EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sbStringToSign EXEC @hr = sp_OADestroy @json EXEC @hr = sp_OADestroy @dt EXEC @hr = sp_OADestroy @crypt EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @queryParams RETURN END EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT PRINT 'response status code = ' + @iTmp0 PRINT 'response body:' EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @resp EXEC @hr = sp_OADestroy @sbStringToSign EXEC @hr = sp_OADestroy @json EXEC @hr = sp_OADestroy @dt EXEC @hr = sp_OADestroy @crypt EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @queryParams END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.