Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
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 Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.OAuth1,
Chilkat.Rest,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
rest: TRest;
json: TJsonObject;
oauth1: TOAuth1;
autoReconnect: Boolean;
tls: Boolean;
jsonText: string;
jsonResponseText: string;
jsonResponse: TJsonObject;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rest := TRest.Create;
// See this example for getting an OAuth1 token for Etsy
json := TJsonObject.Create;
success := json.LoadFile('qa_data/tokens/etsy.json');
if (success = False) then
begin
WriteLn('Failed to load previously fetched Etsy OAuth1 access token.');
Exit;
end;
oauth1 := TOAuth1.Create;
oauth1.ConsumerKey := 'app_keystring';
oauth1.ConsumerSecret := 'app_shared_secret';
oauth1.Token := json.StringOf('oauth_token');
oauth1.TokenSecret := json.StringOf('oauth_token_secret');
oauth1.SignatureMethod := 'HMAC-SHA1';
oauth1.GenNonce(16);
autoReconnect := True;
tls := True;
success := rest.Connect('openapi.etsy.com',443,tls,autoReconnect);
if (success = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
// Tell the REST object to use the OAuth1 object.
success := rest.SetAuthOAuth1(oauth1,True);
jsonText := '[{"product_id":1999949999,"property_values":[],"offerings":[{"offering_id":9999905883,"price":"36.23","quantity":1}]}]';
rest.AddQueryParam('products',jsonText);
rest.AddHeader('Content-Type','application/x-www-form-urlencoded');
jsonResponseText := rest.FullRequestFormUrlEncoded('PUT','/v2/listings/228827035/inventory');
if (rest.LastMethodSuccess = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
jsonResponse := TJsonObject.Create;
jsonResponse.Load(jsonResponseText);
jsonResponse.EmitCompact := False;
WriteLn(jsonResponse.Emit());
WriteLn('Response status code: ' + rest.ResponseStatusCode);
rest.Free;
json.Free;
oauth1.Free;
jsonResponse.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.