Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Configure OAuth 1.0a Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthOAuth1, which associates an OAuth1 provider so Chilkat computes the OAuth 1.0/1.0a signature for each request. The second argument selects whether OAuth parameters are placed in the query string or the Authorization header.
Background. OAuth 1.0a signs each request using the consumer key/secret and the access token/secret. Chilkat generates the nonce, timestamp, and signature automatically for every request sent through the object.
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;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
rest: TRest;
bTls: Boolean;
bAutoReconnect: Boolean;
oauth1: TOAuth1;
bUseQueryParams: Boolean;
responseText: string;
begin
success := False;
rest := TRest.Create;
bTls := True;
bAutoReconnect := True;
success := rest.Connect('example.com',443,bTls,bAutoReconnect);
if (success = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
// Configure OAuth 1.0a request signing. Provide the consumer credentials and the access token
// credentials; Chilkat computes the OAuth signature for each request.
oauth1 := TOAuth1.Create;
// Normally you would not hard-code secrets in source. You should instead obtain them from an
// interactive prompt, environment variable, or a secrets vault.
oauth1.ConsumerKey := 'CONSUMER_KEY';
oauth1.ConsumerSecret := 'CONSUMER_SECRET';
oauth1.Token := 'ACCESS_TOKEN';
oauth1.TokenSecret := 'ACCESS_TOKEN_SECRET';
oauth1.SignatureMethod := 'HMAC-SHA1';
// The 2nd argument selects whether OAuth parameters are placed in the query string (True) or the
// Authorization header (False).
bUseQueryParams := False;
success := rest.SetAuthOAuth1(oauth1,bUseQueryParams);
if (success = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
responseText := rest.FullRequestNoBody('GET','/api/resource');
if (rest.LastMethodSuccess = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
WriteLn(responseText);
rest.Free;
oauth1.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.