SQL Server
SQL Server
Update an Inventory Listing using OAuth1 Authentication
See more Etsy Examples
Updates an inventory listing. This example uses OAuth1 authentication instead of providing an api_key=MY_ETSY_KEYSTRING query parameter.Chilkat SQL Server Downloads
-- 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)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @rest int
EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- See this example for getting an OAuth1 token for Etsy
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'LoadFile', @success OUT, 'qa_data/tokens/etsy.json'
IF @success = 0
BEGIN
PRINT 'Failed to load previously fetched Etsy OAuth1 access token.'
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @json
RETURN
END
DECLARE @oauth1 int
EXEC @hr = sp_OACreate 'Chilkat.OAuth1', @oauth1 OUT
EXEC sp_OASetProperty @oauth1, 'ConsumerKey', 'app_keystring'
EXEC sp_OASetProperty @oauth1, 'ConsumerSecret', 'app_shared_secret'
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'oauth_token'
EXEC sp_OASetProperty @oauth1, 'Token', @sTmp0
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'oauth_token_secret'
EXEC sp_OASetProperty @oauth1, 'TokenSecret', @sTmp0
EXEC sp_OASetProperty @oauth1, 'SignatureMethod', 'HMAC-SHA1'
EXEC sp_OAMethod @oauth1, 'GenNonce', @success OUT, 16
DECLARE @autoReconnect int
SELECT @autoReconnect = 1
DECLARE @tls int
SELECT @tls = 1
EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'openapi.etsy.com', 443, @tls, @autoReconnect
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @oauth1
RETURN
END
-- Tell the REST object to use the OAuth1 object.
EXEC sp_OAMethod @rest, 'SetAuthOAuth1', @success OUT, @oauth1, 1
DECLARE @jsonText nvarchar(4000)
SELECT @jsonText = '[{"product_id":1999949999,"property_values":[],"offerings":[{"offering_id":9999905883,"price":"36.23","quantity":1}]}]'
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'products', @jsonText
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/x-www-form-urlencoded'
DECLARE @jsonResponseText nvarchar(4000)
EXEC sp_OAMethod @rest, 'FullRequestFormUrlEncoded', @jsonResponseText OUT, 'PUT', '/v2/listings/228827035/inventory'
EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @oauth1
RETURN
END
DECLARE @jsonResponse int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonResponse OUT
EXEC sp_OAMethod @jsonResponse, 'Load', @success OUT, @jsonResponseText
EXEC sp_OASetProperty @jsonResponse, 'EmitCompact', 0
EXEC sp_OAMethod @jsonResponse, 'Emit', @sTmp0 OUT
PRINT @sTmp0
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
PRINT 'Response status code: ' + @iTmp0
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @oauth1
EXEC @hr = sp_OADestroy @jsonResponse
END
GO