Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Shopify Private Authentication for Private Apps

See more Shopify Examples

Shopify private authentication is for interacting with your own store through private applications. It uses HTTP "Basic" authentication with your Shopify private application key and secret key.

This example demonstrates how to send a private authenticated request using Chilkat Http, and then the same using Chilkat Rest.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.Http,
  Chilkat.StringBuilder,
  Chilkat.Rest,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  resp: THttpResponse;
  sbJson: TStringBuilder;
  json: TJsonObject;
  rest: TRest;
  bTls: Boolean;
  port: Integer;
  bAutoReconnect: Boolean;

begin
  success := False;

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

  //  First demonstrate sending a simple request using Shopify private authentication w/ the Chilkat Http API.
  http := THttp.Create;

  //  To use HTTP Basic Authentication with any HTTP request, we simply set the Login, Password, and BasicAuth properties.
  //  Important: All HTTP requests using Basic authentication must be over SSL/TLS.
  http.Login := 'SHOPIFY_PRIVATE_API_KEY';
  http.Password := 'SHOPIFY_PRIVATE_API_SECRET_KEY';
  http.BasicAuth := True;

  //  Make sure to replace "chilkat" with your store name.
  resp := THttpResponse.Create;
  success := http.HttpNoBody('GET','https://chilkat.myshopify.com/admin/products.json',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  //  Examine the response code.
  if (resp.StatusCode <> 200) then
    begin
      WriteLn('Received error response code: ' + resp.StatusCode);
      WriteLn('Response body:');
      WriteLn(resp.BodyStr);
      Exit;
    end;

  //  Success.
  WriteLn('Success.');

  //  Examine the JSON response 
  sbJson := TStringBuilder.Create;
  resp.GetBodySb(sbJson);
  json := TJsonObject.Create;
  json.LoadSb(sbJson);
  json.EmitCompact := False;
  WriteLn(json.Emit());

  //  -------------------------------------------------
  //  Now let's do the same using the Chilkat Rest API.
  rest := TRest.Create;

  //  Provide the private app credentials:
  rest.SetAuthBasic('SHOPIFY_PRIVATE_API_KEY','SHOPIFY_PRIVATE_API_SECRET_KEY');

  //  Connect to the shopify server.
  bTls := True;
  port := 443;
  bAutoReconnect := True;
  //  Make sure to replace "chilkat" with your store name.
  success := rest.Connect('chilkat.myshopify.com',port,bTls,bAutoReconnect);
  if (success = False) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;

  sbJson.Clear();
  success := rest.FullRequestNoBodySb('GET','/admin/products.json',sbJson);
  if (rest.LastMethodSuccess = False) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;

  if (rest.ResponseStatusCode <> 200) then
    begin
      WriteLn('Received error response code: ' + rest.ResponseStatusCode);
      WriteLn('Response body:');
      WriteLn(sbJson.GetAsString());
      Exit;
    end;

  //  Success...
  json.LoadSb(sbJson);
  WriteLn(json.Emit());


  http.Free;
  resp.Free;
  sbJson.Free;
  json.Free;
  rest.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.