Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
AzureWebsites OAuth2 Password Flow
See more OAuth2 Examples
Demonstrates how to do OAuth 2.0 password flow for azurewebsites.net.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.Http,
Chilkat.HttpRequest,
Chilkat.HttpResponse,
Chilkat.StringBuilder,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
req: THttpRequest;
tokenEndpoint: string;
resp: THttpResponse;
sbResponseBody: TStringBuilder;
jResp: TJsonObject;
respStatusCode: Integer;
sbXml: TStringBuilder;
destUrl: string;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := THttp.Create;
req := THttpRequest.Create;
req.HttpVerb := 'POST';
req.Path := '/token';
req.ContentType := 'application/x-www-form-urlencoded';
req.AddParam('grant_type','password');
req.AddParam('username','your_username');
req.AddParam('password','your_password');
tokenEndpoint := 'https://your_api.azurewebsites.net/token';
resp := THttpResponse.Create;
success := http.HttpReq(tokenEndpoint,req,resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
sbResponseBody := TStringBuilder.Create;
resp.GetBodySb(sbResponseBody);
jResp := TJsonObject.Create;
jResp.LoadSb(sbResponseBody);
jResp.EmitCompact := False;
WriteLn('Response Body:');
WriteLn(jResp.Emit());
// Sample JSON response:
// {
// "access_token": "NQGHn ... xTS",
// "token_type": "bearer",
// "expires_in": 1209599,
// "userName": "your_username",
// ".issued": "Mon, 27 Apr 2020 23:49:35 GMT",
// ".expires": "Mon, 11 May 2020 23:49:35 GMT"
// }
respStatusCode := resp.StatusCode;
WriteLn('Response Status Code = ' + respStatusCode);
if (respStatusCode >= 400) then
begin
WriteLn('Response Header:');
WriteLn(resp.Header);
WriteLn('Failed.');
Exit;
end;
// ----------------------------------
// Use the OAuth2 token in a request.
// For example...
sbXml := TStringBuilder.Create;
success := sbXml.LoadFile('c:/someDir/someXmlFile.xml','utf-8');
if (success = False) then
begin
WriteLn('Failed to load the XML file.');
Exit;
end;
// Get the OAuth2 token and use it for authentication
http.AuthToken := jResp.StringOf('token');
destUrl := 'https://your_api.azurewebsites.net/destinationUrl';
success := http.HttpSb('POST',destUrl,sbXml,'utf-8','application/xml',resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
respStatusCode := resp.StatusCode;
WriteLn('Response Status Code = ' + respStatusCode);
if (respStatusCode >= 400) then
begin
WriteLn('Response Header:');
WriteLn(resp.Header);
WriteLn('Failed.');
Exit;
end;
// Examine the response body
WriteLn(resp.BodyStr);
http.Free;
req.Free;
resp.Free;
sbResponseBody.Free;
jResp.Free;
sbXml.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.